【问题标题】:TypeConverter from string to UserControlTypeConverter 从字符串到 UserControl
【发布时间】:2013-10-24 10:29:56
【问题描述】:

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

我想使用语法<c:MyListBoxItem UserControl="Test">Information</c:MyListBoxItem>,但我不确定如何将字符串“Test”或“local:Test”的类型转换器写入该 xaml 页面上的 usercontrol Test。

回答“nit”的评论:

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

&lt;c:MyListBoxItem UserControl="{StaticResource Test}"&gt;Information&lt;/c:MyListBoxItem&gt; 一起工作。 但是我想要常规 xaml 定义中的 UserControl 并找到了另外两种方法:

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

但是x:Reference 给出了编译时错误:方法/操作未实现。它仍然运行,顺便说一句,imo 很奇怪。并且:

<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>

【问题讨论】:

    标签: c# wpf xaml type-conversion typeconverter


    【解决方案1】:

    我不确定您想通过这样做来实现什么,但您必须将该 UserControl 放入资源中

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

    然后你可以将你的 DP 设置为

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

    【讨论】:

    • 这行得通!设置了 usercontrol 属性,这是一件好事。但是我在 SelectionChanged 事件中注意到 UserControl 的 Content 和 Name 属性为空??
    • 你能分享一下你是如何在资源中设置用户控件的名称和内容的吗?
    • 对不起,它有效,我忘了给用户控件 x:Name 属性。请参阅我的编辑以获得更好的解决方案。
    【解决方案2】:

    如果你想使用实际的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 的默认(空)构造函数。

    【讨论】:

    • 是否可以代替Activator.CreateInstance(userControlType)找到已在xaml 中声明的现有用户控件?
    • 一切皆有可能,但我不建议在TypeConverter 中这样做。如果您想这样做,那么我建议您使用 nit 为您提供的方法。
    • @Gerard,如果有帮助,您可以使用Activator 类调用特定的构造函数,您可以在其中执行一些设置功能。
    猜你喜欢
    • 2012-08-13
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    相关资源
    最近更新 更多