设置列表框框内容的动态

0

的问题

我有一个WPF用户的控制,这是一个 ListBox 包含 CheckBoxes.

<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 可以是任何东西。 在这一点上,我有这个位置的 ContentCheckBox 但大多数时候这种结果的一些含糊不清喜欢 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>
binding c# wpf
2021-11-23 14:51:41
1

最好的答案

1

增加一个 CheckBoxContent 依赖酒店UserControl's代码隐藏文件:

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>
2021-11-24 15:02:23

利率的答复。 我已经做了像你这样的建议,但将如何我现在填写在这 CheckBoxContent 当使用 CheckedListBox? 采取一类 Company 例如有一个酒店 Name. 我已经试过了以下,但内容是空的: <user:CheckedListBox ItemsSource="{Binding ElementName=Root, Path=Companies}" CheckBoxContent="{Binding Name}" />.
Krowi

它的工作如果你设置的财产,以恒串值,e。g: CheckBoxContent="test"? 然后结合 Name 失败。
mm8

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................