0

I have a WPF user control which is a ListBox that contains 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>

I'm using this control at another place:

<GroupBox Header="{Binding ElementName=Root, Path=Header, FallbackValue=Header}">
    <user:CheckedListBox ItemsSource="{Binding ElementName=Root, Path=Companies}" />
</GroupBox>

At this place, the Companies property is an ObservableCollection<CheckedListBoxItem<Company>>.

CheckedListBoxItem is a class with bool IsChecked property and generic T Item property.

Now, T Item can be anything. At this point I have this property set as the Content of the CheckBox but most times this results in something ambiguous like AC.CodA.Scripting.Shared.Company.

Is there a way to set the CheckBox Content outside of the user control? Something like this with CheckBoxContent:

<GroupBox Header="{Binding ElementName=Root, Path=Header, FallbackValue=Header}">
    <user:CheckedListBox ItemsSource="{Binding ElementName=Root, Path=Companies}" CheckBoxContent="{Binding Item.Name}" />
</GroupBox>

and inside my object this:

<CheckBox IsChecked="{Binding IsChecked}"
          Content="{Binding CheckBoxContent}">
</CheckBox>

1 Answer 1

1

Add a CheckBoxContent dependency property to your UserControl's code-behind file:

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); }
    }
}

...and bind to it in your XAML markup:

<CheckBox IsChecked="{Binding IsChecked}"
          Content="{Binding CheckBoxContent,
              RelativeSource={RelativeSource AncestorType=UserControl}}">
</CheckBox>
Sign up to request clarification or add additional context in comments.

3 Comments

Tnx for the reply. I've done this like you suggested but how would I now fill in this CheckBoxContent when using the CheckedListBox? Take a class Company for example with a property Name. I've tried the following but the content is empty: <user:CheckedListBox ItemsSource="{Binding ElementName=Root, Path=Companies}" CheckBoxContent="{Binding Name}" />.
Does it work if you set the property to a constant string value, e.g: CheckBoxContent="test"? Then the binding to Name fails.
Tnx for the help but the project has moved on and decided to use another approach. Sorry for wasting your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.