Question

I have an ItemsControl in which I display different properties and values, with the name on one side and a TextBox on the other side. The ItemsSource is a collection of objects of a custom class, that has Name, Value and PropertyType properties (using reflections propertyinfo)

Now I would like to improve this by being able to detect whether the property is of type bool for example, which would display a checkbox instead of a textbox. Is this possible using a DataTrigger?

I got it semi-working using a Control of which I set the template to a textbox or checkbox according to the type, but when I try to "tab" to the next textbox or checkbox, it focuses the control that has the textbox/checkbox first, and only after another "tab" it focuses the containing textbox/checkbox/..

So if anybody know a solution for this, that would be greatly appreciated!

Was it helpful?

Solution

Use the solution you already have and set the Focusable property to false on the control that wrongly gets tab focus.

OTHER TIPS

You can use DataTemplate to select different View based on Value property type.

View:

<ItemsControl ItemsSource="{Binding Path=Options}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <DataTemplate.Resources>
                        <DataTemplate DataType="{x:Type System:Boolean}">
                            <CheckBox IsChecked="{Binding Path=.}"/>
                        </DataTemplate>
                        <DataTemplate DataType="{x:Type System:String}">
                            <TextBox Text="{Binding Path=.}"/>
                        </DataTemplate>
                    </DataTemplate.Resources>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name, Mode=OneWay}"/>
                        <ContentControl Content="{Binding Path=Value}"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

ViewModel:

public class MainViewModel
{
    public ArrayList Options { get; set; }

    public MainViewModel()
    {
        Options = new ArrayList();
        Options.Add(new TextProperty());
        Options.Add(new BoolProperty());
    }

}

public class TextProperty
{
    public string Name { get; set; }

    public string Value { get; set; }

    public TextProperty()
    {
        Name = "Name";
        Value = "Default";
    }
}

public class BoolProperty
{
    public string Name { get; set; }

    public bool Value { get; set; }

    public BoolProperty()
    {
        Name = "IsEnabled";
        Value = true;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top