简体   繁体   中英

Binding in DataTemplate/ItemTemplate

I have a ComboBox whose ItemSource is a ListCollectionView of MyClass. The ComboBox uses the following ItemTemplate:

<Style x:Key="StyleComboBoxGroups" BasedOn="{StaticResource BaseComboBox}" TargetType="{x:Type ComboBox}">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <BulletDecorator VerticalAlignment="Center">
                        <BulletDecorator.Bullet>
                            <Ellipse Margin="3,0,0,0" Width="4" Height="4"/>
                        </BulletDecorator.Bullet>
                        <TextBlock Margin="3,0,0,0" Style="{StaticResource BaseTextBlock}" Text="{Binding}"/>
                    </BulletDecorator>
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

MyClass exposes two properties:

public sealed class MyClass { public MyEnum Property1 { get; set; } public String Property2 { get; set; } }

I'm trying to figure out how to make the TextBlock.Foreground color depending on Property1. For the moment I tried these to no avail:

Foreground="{Binding Property1, Converter=MyEnumToColorConverter}"
Foreground="{Binding Converter=MyEnumToColorConverter, Path=Property1}"
Foreground="{Binding Converter=MyEnumToColorConverter, Path=Property1, Source={Binding}}"

But I always get an Exception. How can I bind to the ComboBox item properly?

Declare a resource that will be an instance of your converter.

<Style.Resources>
    <namespace:MyEnumToColorConverter x:Key="MyEnumToColorConverter" />
</Style.Resources>

… then use that resource in your binding:

Foreground="{Binding Property1, Converter={StaticResource MyEnumToColorConverter}}"

An alternative is to reference your converter statically, in which case you will not need to create a resource instance. Turn your converter into a singleton:

public class MyEnumColorConverter : IValueConverter
{
    private MyEnumColorConverter() { }

     private static IValueConverter _instance;
     public static IValueConverter Instance
     {
          get { return _instance ?? (_instance = new MyEnumColorConverter); }
     }

     // implement IValueConverter

}

In XAML you will reference it like this:

Foreground="{Binding Property1, Converter={x:Static namespace:MyEnumColorConverter.Instance}}"

If you in fact have a converter of type MyEnumToColorConverter , then in order to use it in a binding, you'll need to first declare an instance of it. Typically, you'll do that in the Resources section of the parent control (or style,) something like:

<Style.Resources>
    <myNamespace:MyEnumToColorConverter x:Key="EnumToColorConverter" />
</Style.Resources>

Then, you can reference it with a StaticResource markup extension:

Foreground="{Binding Path=Property1, Converter={StaticResource EnumToColorConverter}}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM