xaml ウィンドウに、依存関係プロパティtypeof を 1 つだけ追加し<UserControl x:Name="Test">...
たカスタムがあるとします。 MyListBoxItem
UserControlProperty
UserControl
構文を使用したいのですが<c:MyListBoxItem UserControl="Test">Information</c:MyListBoxItem>
、文字列「Test」またはおそらく「local:Test」からその xaml ページのユーザーコントロール 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 が必要であり、それを行う他の 2 つの方法を見つけました。
<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>