2014-09-22 19 views
0

我有一个嵌套列表框(在另一个列表框中的列表框)。我想为突出显示的/选中的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); 
      } 
     } 

字符串刷转换器类

每当转换器被调用时,对象值是空字符串 “”。在调试时,我发现属性SelectedItemForegroundColor的值不是空的。我已经分配了绿色,并且仍然保持这个值为绿色。

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

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

在此先感谢

+1

显然,您使用了错误的“绑定路径”。你的两个属性在哪里声明,这个XAML在哪里声明? – Sheridan 2014-09-22 15:00:16

+1

'SelectedResultItemForegroundColor'和'SelectedItemForegroundColor'拼写有所不同,是否有意或无意? – Kcvin 2014-09-22 15:16:06

+0

@Netscape:对不起,属性名称是SelectedItemForegroundColor而不是SelectedResultItemForegroundColor。另一个属性是SelectedItemFontWeight。 – Shiva 2014-09-23 02:25:14

回答

0

我使用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}}"/> 

它现在工作的罚款。

谢谢大家。