【问题标题】:Set ListBox CheckBox Content dynamically动态设置 ListBox CheckBox 内容
【发布时间】:2022-01-02 01:41:38
【问题描述】:

我有一个 WPF 用户控件,它是一个包含 CheckBoxes 的 ListBox

<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&lt;CheckedListBoxItem&lt;Company&gt;&gt;

CheckedListBoxItem 是一个具有bool IsChecked 属性和通用T Item 属性的类。

现在,T Item 可以是任何东西。此时我将此属性设置为CheckBoxContent,但大多数情况下这会导致类似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>

【问题讨论】:

    标签: c# wpf binding


    【解决方案1】:

    CheckBoxContent dependency property 添加到您的UserControl 的代码隐藏文件中:

    public partial class CheckedListBox : UserControl
    {
        ...
    
        public static readonly DependencyProperty CheckBoxContentProperty =
            DependencyProperty.Register(nameof(CheckBoxContent), typeof(object),
                typeof(CheckedListBox));
    
        public object CheckBoxContent
        {
            get { return GetValue(CheckBoxContentProperty); }
            set { SetValue(CheckBoxContentProperty, value); }
        }
    }
    

    ...并在您的 XAML 标记中绑定到它:

    <CheckBox IsChecked="{Binding IsChecked}"
              Content="{Binding CheckBoxContent,
                  RelativeSource={RelativeSource AncestorType=UserControl}}">
    </CheckBox>
    

    【讨论】:

    • Tnx 回复。我已经按照您的建议完成了此操作,但是在使用 CheckedListBox 时,我现在如何填写此 CheckBoxContent?以类Company 为例,其属性为Name。我尝试了以下但内容为空:&lt;user:CheckedListBox ItemsSource="{Binding ElementName=Root, Path=Companies}" CheckBoxContent="{Binding Name}" /&gt;.
    • 如果将属性设置为常量字符串值是否有效,例如:CheckBoxContent="test"?然后绑定到Name 失败。
    • Tnx 寻求帮助,但该项目已继续进行并决定使用另一种方法。很抱歉浪费了您的时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2019-07-13
    • 2015-07-06
    相关资源
    最近更新 更多