我有一个WPF用户的控制,这是一个 ListBox
包含 CheckBox
es.
<UserControl x:Class="AC.CodA.WPF.UserControls.CheckedListBox"
x:Name="Root"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:Background="White">
<StackPanel>
<ListBox BorderThickness="0"
ItemsSource="{Binding ElementName=Root, Path=ItemsSource}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable"
Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}"
Content="{Binding Item}">
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Separator />
<CheckBox x:Name="DeSelectAll"
Content="Select / deselect all"
Checked="DeSelectAll_Checked"
Unchecked="DeSelectAll_Unchecked" />
</StackPanel>
</UserControl>
我使用这个控制在另一个地方:
<GroupBox Header="{Binding ElementName=Root, Path=Header, FallbackValue=Header}">
<user:CheckedListBox ItemsSource="{Binding ElementName=Root, Path=Companies}" />
</GroupBox>
在这个地方, Companies
是一个酒店 ObservableCollection<CheckedListBoxItem<Company>>
.
CheckedListBoxItem
是一类的 bool IsChecked
酒店和一般 T Item
财产。
现在, T Item
可以是任何东西。 在这一点上,我有这个位置的 Content
的 CheckBox
但大多数时候这种结果的一些含糊不清喜欢 AC.CodA.Scripting.Shared.Company
.
有没有一种方法来设定 CheckBox
Content
外部的用户控制? 这样的事情与 CheckBoxContent
:
<GroupBox Header="{Binding ElementName=Root, Path=Header, FallbackValue=Header}">
<user:CheckedListBox ItemsSource="{Binding ElementName=Root, Path=Companies}" CheckBoxContent="{Binding Item.Name}" />
</GroupBox>
和在我象这样:
<CheckBox IsChecked="{Binding IsChecked}"
Content="{Binding CheckBoxContent}">
</CheckBox>
CheckBoxContent
当使用CheckedListBox
? 采取一类Company
例如有一个酒店Name
. 我已经试过了以下,但内容是空的:<user:CheckedListBox ItemsSource="{Binding ElementName=Root, Path=Companies}" CheckBoxContent="{Binding Name}" />
.