gpt4 book ai didi

.net - 如何创建 Type 类型的依赖属性,并在 XAML 中分配它?

转载 作者:行者123 更新时间:2023-12-02 06:11:25 25 4
gpt4 key购买 nike

我想知道如何在 XAML 中分配 Silverlight 中 Type 类型的依赖属性,因为标记扩展 {x:Type} 不存在?

谢谢

最佳答案

根据您的要求,可能可以采取一系列不同的方法。以下是非常通用的解决方案。

创建一个将字符串转换为类型的值转换器:-

public class StringToTypeConverter : IValueConverter
{

#region IValueConverter Members

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Type.GetType((string)value);
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}

#endregion
}

将此转换器的实例放置在目标对象具有可见性的资源字典中,例如 App.xaml:-

    <Application.Resources>
<local:StringToTypeConverter x:Key="STT" />
</Application.Resources>

现在,在 Xaml 中,您可以为属性分配一个值,如下所示:-

 <TextBox Text="{Binding Source='System.Int32,mscorlib', Converter={StaticResource STT}}" />

关于.net - 如何创建 Type 类型的依赖属性,并在 XAML 中分配它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2445970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com