Имеется ComboBox:
<ComboBox x:Name="MyComboBox"
ItemsSource="{Binding MyObservableObject}"
ItemContainerStyle="{StaticResource ComboBoxItemStyle}"
DisplayMemberPath="Value"/>
MyObservableObject это:
ObservableCollection<MyClass> где MyClass это объекты типа:
class MyClass
{
public string Value {get; set;}
public string OtherValue {get; set;}
}
ComboboxItemStyle это стиль:
<Style x:Key="ComboBoxItemStyle" TargetType="ComboBoxItem">
<Setter Property="FontWeight">
<Setter.Value>
<Binding Converter="{StaticResource MyFontConverter}"/>
</Setter.Value>
</Setter>
</Style>
Соотвественно, класс конвертера MyFontConverter переводит объекты MyClass в FontWeight
Это прекрасно работает для ComboBoxItems.
Но мне надо установить FontWeight у самого ComboBox, в зависимости от того элемента, который отображается. Пытаюсь делать так:
<ComboBox x:Name="MyComboBox"
ItemsSource="{Binding MyObservableObject}"
ItemContainerStyle="{StaticResource ComboBoxItemStyle}"
DisplayMemberPath="Value">
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="FontWeight">
<Setter.Value>
<Binding Converter="{StaticResource MyFontConverter}"/>
</Setter.Value>
</Setter>
</Style>
</ComboBox.Style>
</ComboBox>
Но в конвертер попадает сама ViewModel
Например, так:
FontWeight="{Binding SelectedItem, RelativeSource={RelativeSource Self}, Converter={StaticResource MyFontConverter}}"
Или так:
FontWeight="{Binding MyObservableObject/, Converter={StaticResource MyFontConverter}}"
Сборка персонального компьютера от Artline: умный выбор для современных пользователей