Shiva
Shiva

Reputation: 31

Binding Value in Style Setter not working

I have a nested listbox (listbox within another listbox). I want to set the foreground color property for the highlighted/selected listboxitem and also the fontweight. The Value for the Color and font weight are read from a xml file. The SelectedItemForegroundColor and the SelectedItemFontWeight properties are set as string when the view model constructor gets executed. These properties are set only once and wont be changing later anytime unless the values in xml file are updated and the application is restarted.

Here is the XAML code snippet for the problem.

<Style TargetType="ListBoxItem">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="White"/>
            </Style.Resources>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Foreground" Value="{Binding SelectedItemForegroundColor, Converter={StaticResource stringToBrushConverter}}"/>
                    <Setter Property="FontWeight" Value="{Binding SelectedItemFontWeight}"/>
                </Trigger>
            </Style.Triggers>
        </Style>



<ListBox ItemsSource="{Binding ItemsList}" SelectedItem="{Binding SelectedResultItem}" SelectionChanged="OnListBoxSelectionChanged" Background="White" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,5">
                        <ListBox ItemsSource="{Binding InnerItems}" BorderThickness="0" Background="White"
                                 Foreground="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding LabelName}" Margin="0,0,5,0"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

This is the Property used to set the foreground color during initialization of view model.

public string SelectedItemForegroundColor
        {
            get
            {
                return this.selectedItemForegroundColor;
            }

            set
            {
                this.selectedItemForegroundColor = value;
                this.RaisePropertyChanged(() => this.SelectedItemForegroundColor);
            }
        }

public string SelectedItemFontWeight
        {
            get
            {
                return this.selectedItemFontWeight;
            }

            set
            {
                this.selectedItemFontWeight = value;
                this.RaisePropertyChanged(() => this.SelectedItemFontWeight);
            }
        }

The string to brush converter class:

Whenever converter is called, the object value is empty string "". While debugging I found the property SelectedItemForegroundColor value was not empty. I had assigned Green and it was still holding this value as Green .

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var brush = DefaultBrush;
            if (!string.IsNullOrEmpty(value.ToString()))
            {
                var color = Color.FromName(value.ToString());                
                brush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B));
            }

            return brush;
        }

The value of the properties are not assigned to Background. Also I need to know what converter should we use for changing the font weight of the font.

Thanks in Advance

Upvotes: 0

Views: 4978

Answers (1)

Shiva
Shiva

Reputation: 31

I used the Snoop to find the binding issues. Found that the outer listbox was getting the DataContext but was not accessible to the inner listbox. I replaced the below code

<Setter Property="Foreground" Value="{Binding SelectedItemForegroundColor, Converter={StaticResource stringToBrushConverter}}"/>

with

<Setter Property="Foreground" Value="{Binding Path=DataContext.SelectedResultItemForegroundColor, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"/>

Its working fine now.

Thanks everyone.

Upvotes: 3

Related Questions