Wpf 将listboxitem内容绑定到listbox的dependency属性

Wpf 将listboxitem内容绑定到listbox的dependency属性,wpf,data-binding,listbox,listboxitem,checkedlistbox,Wpf,Data Binding,Listbox,Listboxitem,Checkedlistbox,我正在尝试使用自定义checkedlistbox项在WPF中创建自定义控件checkedlistbox checkedlistboxitem的Xaml代码: <Style TargetType="{x:Type local:CheckedListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> <Setter Property="Template"> <Setter.Valu

我正在尝试使用自定义checkedlistbox项在WPF中创建自定义控件checkedlistbox

checkedlistboxitem的Xaml代码:

<Style TargetType="{x:Type local:CheckedListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CheckedListBoxItem}">
                <CheckBox VerticalContentAlignment="Center" Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=Content}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
选中的listboxitem类:

public class CheckedListBoxItem : ListBoxItem
{
    static CheckedListBoxItem()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckedListBoxItem), new FrameworkPropertyMetadata(typeof(CheckedListBoxItem)));
    }



}
并且iam在具有以下xaml代码的窗口中使用它:

<GroupBox Grid.Column="2" Style="{StaticResource GroupBoxStyle}"  Header="Choose Categories :" >
        <local:CheckedListBox ItemsSource="{Binding AllCategories}" ItemBindingName="{Binding Name}"  />

    </GroupBox>

我想要的是:

为了使customcheckedlistbox具有通用性,以便我不必在模板本身中输入绑定到的属性的名称(即,我想从模板中删除Content=“{binding name}”部分),我创建了一个名为“ItemBindingName”的依赖性属性对于checkedlistbox&我希望项模板绑定其名称位于“ItemBindingName”属性中的属性(即,当我在checkedlistbox声明中写入ItemBindingName=“{Binding xxx}”时,模板自动绑定到“xxx”属性)

对不起,如果我的问题听起来不太容易理解,
我真的尽力简化了它,这是我能得到的最好的方法。

您的CheckedListBox类应该重写以下方法。在
PrepareContainerForItemOverride
中,它将建立内容绑定

protected override DependencyObject GetContainerForItemOverride()
{
    return new CheckedListBoxItem();
}

protected override bool IsItemItsOwnContainerOverride(object item)
{
    return item is CheckedListBoxItem
        || base.IsItemItsOwnContainerOverride(item);
}

protected override void PrepareContainerForItemOverride(
    DependencyObject element, object item)
{
    if (!string.IsNullOrEmpty(ItemBindingName))
    {
        ((ContentControl)element).SetBinding(
            ContentControl.ContentProperty, new Binding(ItemBindingName));
    }
}

请注意,CheckedListBoxItem的ControlTemplate中的内容绑定可能如下所示:

<CheckBox Content="{TemplateBinding Content}" ... />


为什么不在常规列表框的ItemTemplate中添加一个复选框?似乎不需要派生ListBox和ListBoxItem。CheckedListBoxItem模板中甚至没有复选框的IsChecked属性的绑定。@Clemens我稍后将添加IsChecked属性以及我需要的许多其他特定功能(使用键盘跳转到特定选择,并将选中的项目与可观察集合绑定…等等)但目前,作为其通用行为的一部分,我需要定义iam绑定到模板外部的属性(在listbox声明中)
protected override DependencyObject GetContainerForItemOverride()
{
    return new CheckedListBoxItem();
}

protected override bool IsItemItsOwnContainerOverride(object item)
{
    return item is CheckedListBoxItem
        || base.IsItemItsOwnContainerOverride(item);
}

protected override void PrepareContainerForItemOverride(
    DependencyObject element, object item)
{
    if (!string.IsNullOrEmpty(ItemBindingName))
    {
        ((ContentControl)element).SetBinding(
            ContentControl.ContentProperty, new Binding(ItemBindingName));
    }
}
<CheckBox Content="{TemplateBinding Content}" ... />