从字符串到 UserControl 的 TypeConverter [英] TypeConverter from string to UserControl
问题描述
假设在一个 xaml 窗口中我有 <UserControl x:Name="Test">...
我有一个自定义 MyListBoxItem
,只添加了一个依赖属性 UserControlProperty
,typeof UserControl
.
Suppose in a xaml window I have <UserControl x:Name="Test">...
I have a custom MyListBoxItem
with only one dependencyproperty UserControlProperty
added, typeof UserControl
.
我想使用语法 <c:MyListBoxItem UserControl="Test">Information</c:MyListBoxItem>
但我不知道如何从字符串 "Test" 或者可能是 "local:Test" 到该 xaml 页面上的用户控件测试.
I want to use the syntax <c:MyListBoxItem UserControl="Test">Information</c:MyListBoxItem>
and I am not sure how to write a typeconverter from the string "Test" or perhaps "local:Test" to the usercontrol Test on that xaml page.
回答'nit'的评论:
In answer to a comment of '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 并找到了另外两种方法:
with <c:MyListBoxItem UserControl="{StaticResource Test}">Information</c:MyListBoxItem>
works.
However I wanted the UserControl in the regular xaml definitions and found two other ways of doing it:
<c:MyListBoxItem UserControl="{x:Reference Test}">
但是 x:Reference
给出了编译时错误:方法/操作未实现.它仍然在运行,顺便说一句,imo很奇怪.并且:
However x:Reference
gives a complie time error: method/operation not implemented. It still runs which btw is weird imo. And:
<c:MyListBoxItem UserControl="{Binding ElementName=Test}"
这是一个很好的解决方案.
which is the good solution.
至于你可以通过这个实现什么:
As for what you can achieve by this:
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 定义也很清楚:
This is a nice way to support this kind of menu structure and the xaml definition is also clarifying:
<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 放在资源中
I am not sure what you want to achieve by doing this, but you will have to put that UserControl in the resources
<Window.Resources>
<UserControl x:Key="Test" x:Shared="false">
</Window.Resources>
然后你可以将你的DP设置为
Then you can set your DP as
<c:MyListBoxItem UserControl="{StaticResource Test}">Information</c:MyListBoxItem>
这篇关于从字符串到 UserControl 的 TypeConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!