1

文字列の配列にバインドされたリストボックスがあります。リストボックスには、配列内の文字列のテキストを含むテキストブロックが含まれています。それらの1つの前景を変更したい(どれが異なる場合があります):

 <ListBox x:Name="listBox" ItemsSource="{Binding Options}" ScrollViewer.VerticalScrollBarVisibility="Hidden" Width="400" Height="500" Margin="0,200,0,0" HorizontalAlignment="Center" HorizontalContentAlignment="Center" SelectionChanged="ListBox_SelectionChanged" Loaded="listBox_Loaded">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <ListBoxItem>
                        <Grid Height="75" Width="400" HorizontalAlignment="Center" >
                            <TextBlock HorizontalAlignment="Center" Text="{Binding}" Style="{StaticResource SortingOptions}" />
                        </Grid>
                    </ListBoxItem>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

テキストブロックを手に入れることができないように見えるので、右側の前景を変更できます。誰かが私がこれを達成する方法を知っていますか?ありがとう

4

1 に答える 1

2

ForegroundプロパティをTextと同じ値にバインドし、BindingConverterを使用してBrushを作成します。例えば

<Grid.Resources>
  <yournamespace:ColorConverter x:Key="colConverter"/>
<Grid.Resources>


<TextBlock 
  HorizontalAlignment="Center"
  Text="{Binding}"
  Foreground="{Binding, Converter={StaticResource colConverter}}"
  Style="{StaticResource SortingOptions}" />

コンバータークラスを追加します。

  public class ColorConverter : IValueConverter
  {
  public object  Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
      // TODO: match from the value parameter to a color.

      return new SolidColorBrush(Colors.Red);
  }

  public object  ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
      throw new NotImplementedException();
  }
}
于 2012-07-25T12:33:49.917 に答える