Tomasz
Tomasz

Reputation: 2061

wpf control containing ComboBox and Items setter

with WPF I can do soemthing like this:

<Combobox>
    <ComboBox.Items>
        <system:Boolean>True</system:Boolean>
        <system:Boolean>False</system:Boolean>
    </ComboBox.Items>
</Combobox>

But I have problem with creating a custom control, which will delegate Items to ComboBox

When I do something like this:

<Combobox Items={Binding something}/>

I receive compilation error about Items setter (because Items have no setter). How can I handle that? I mean, set/rewrite items from my control to combobox?

Upvotes: 2

Views: 195

Answers (2)

James Harcourt
James Harcourt

Reputation: 6379

To define the list in XAML to binding, use a ResourceDictionary. This could be in a separate file, but below I have included it in Window.Resources:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary x:Key="boolArray">
            <sys:Boolean x:Key="true">True</sys:Boolean>
            <sys:Boolean x:Key="false">False</sys:Boolean>
        </ResourceDictionary>
    </ResourceDictionary>
</Window.Resources>

Then you can apply the XAML-defined array to the ItemsSource property of your ComboBox:

<ComboBox ItemsSource="{Binding Values, Source={StaticResource boolArray}}"/>

In case it isn't clear, the sys namespace is defined as follows:

The sys namespace is defined as follows:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

Upvotes: 2

Oleg
Oleg

Reputation: 1458

Try this

public class Test
{
    public string T { get; set; }
}

public MainWindow()
{
    InitializeComponent();

    this.DataContext = new List<Test>()
    {
        new Test(){T = "1"}
    };
}

<ComboBox  ItemsSource="{Binding T}"/>

Upvotes: 0

Related Questions