类型转换器从字符串到用户控件

本文关键字:用户 控件 字符串 类型转换 转换器 类型 | 更新日期: 2023-09-27 18:36:38

>假设在 xaml 窗口中我有<UserControl x:Name="Test">...我有一个自定义MyListBoxItem,只UserControlProperty添加一个依赖项属性,类型UserControl

我想使用语法<c:MyListBoxItem UserControl="Test">Information</c:MyListBoxItem>,但我不确定如何将类型转换器从字符串"Test"或"local:Test"编写到该 xaml 页面上的用户控件测试。

在回答"nit"的评论时:

<Window.Resources>
    <UserControl x:Key="Test" x:Name="Test"
                 x:Shared="False">
        <Button Height="50"
                Width="50" />
    </UserControl>
</Window.Resources>

<c:MyListBoxItem UserControl="{StaticResource Test}">Information</c:MyListBoxItem>作品。但是,我希望在常规 xaml 定义中使用 UserControl,并找到了另外两种方法:

<c:MyListBoxItem UserControl="{x:Reference Test}">

但是x:Reference会给出合规时间错误:方法/操作未实现。它仍然运行,顺便说一句,这很奇怪。和:

<c:MyListBoxItem UserControl="{Binding ElementName=Test}"

这是一个很好的解决方案。

至于你可以通过这个实现什么:

private void Menu_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  foreach (var item in e.RemovedItems)
  {
    // collapse usercontrol
    UserControl uc = (item as MyListBoxItem).UserControl;
    if (uc != null) uc.Visibility = Visibility.Collapsed;
            }
  foreach (var item in e.AddedItems)
  {
     // uncollapse usercontrol
     UserControl uc = (item as MyListBoxItem).UserControl;
     if (uc != null) uc.Visibility = Visibility.Visible;
  }
}

这是支持这种菜单结构的好方法,xaml 定义也阐明了:

<c:MyListBoxItem UserControl="{Binding ElementName=Information}" IsSelected="True">Information</c:MyListBoxItem>
<c:MyListBoxItem UserControl="{Binding ElementName=Edit}" IsSelected="False">Edit</c:MyListBoxItem>
<Grid>
   <UserControl x:Name="Information" Visibility="Visible"><Button Content="Placeholder for usercontrol Information" /></UserControl>
   <UserControl x:Name="Edit" Visibility="Collapsed"> <Button Content="Placeholder for usercontrol Edit" /></UserControl>

类型转换器从字符串到用户控件

我不确定这样做要实现什么,但是您必须将该UserControl放在资源中

<Window.Resources>
 <UserControl x:Key="Test" x:Shared="false">
</Window.Resources>

然后,您可以将DP设置为:

    <c:MyListBoxItem UserControl="{StaticResource Test}">Information</c:MyListBoxItem>

如果你想使用一个实际的TypeConverter,你应该能够做这样的事情:

public class UserControlConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string)) return true;
        return base.CanConvertFrom(context, sourceType);
    }
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        if (value is string)
        {
            Type userControlType = Type.GetType(value.ToString(), true, true);
            return Activator.CreateInstance(userControlType);
        }
        return base.ConvertFrom(context, culture, value);
    }
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(UserControl))
        {
            return destinationType.FullName;
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

我没有机会对此进行测试,所以如果您有任何问题,请告诉我。另外,请注意,您将需要使用类型的全名,因此如下所示: ApplicationName.ProjectOrFolderNameIfApplicable.ControlName .另请注意,这将只调用UserControl的默认(空)构造函数。