Style Setter中的绑定值不起作用

时间:2014-09-22 14:42:20

标签: c# css wpf xaml listbox

我有一个嵌套的列表框(另一个列表框中的列表框)。我想为突出显示/选中的listboxitem以及fontweight设置前景色属性。从xml文件中读取颜色和字体粗细的值。在执行视图模型构造函数时,SelectedItemForegroundColor和SelectedItemFontWeight属性设置为字符串。这些属性只设置一次,并且不会随时更改,除非更新xml文件中的值并重新启动应用程序。

以下是此问题的XAML代码段。

<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>

这是用于在视图模型初始化期间设置前景色的属性。

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);
            }
        }

要转换的字符串转换器类

每当调用converter时,对象值为空字符串“”。在调试时我发现属性SelectedItemForegroundColor值不为空。我已经分配了Green,它仍然保持这个值为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;
        }

属性的值未分配给Background。此外,我需要知道我们应该使用什么转换器来改变字体的字体重量。

先谢谢

1 个答案:

答案 0 :(得分:1)

我使用Snoop查找绑定问题。发现外部列表框正在获取DataContext但内部列表框无法访问。我替换了下面的代码

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

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

现在工作正常。

谢谢大家。