0

假设在一个 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>

<c:MyListBoxItem UserControl="{StaticResource Test}">Information</c:MyListBoxItem>作品。但是,我希望常规 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>
4

2 回答 2

1

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

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

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

    <c:MyListBoxItem UserControl="{StaticResource Test}">Information</c:MyListBoxItem>
于 2013-10-24T10:35:11.953 回答
1

如果您想使用实际的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.

于 2013-10-24T10:57:10.477 回答