설 목록 상자에 체크 박스 콘텐츠를 동적으로

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 종속성 제공UserControls 코드-뒤에 파일:

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ý
..................................................................................................................