2017-01-22 135 views
0

我在列表視圖內部有一個textblock,在該列表視圖中,我想要動態地或通過某種方式爲某些不同的文本塊的背景色綁定某些字符。爲某些字符着色

由於字符是如何上色的一些邏輯,我該怎麼做?

附上我的XAML:

<ListView x:Name="LV" ItemsSource= "{Binding Lggv}" SelectionChanged="dataGridData_SelectionChanged" ItemContainerStyle="{StaticResource ListViewItemStyle}" > 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <Border BorderThickness="1" BorderBrush="Black"> 
        <Border.Visibility> 
         <MultiBinding Converter="{StaticResource V_converter }"> 
          <Binding Path="Type"></Binding> 
          <Binding Path="NodeID"></Binding> 
          <Binding Path="TLV"></Binding> 
         </MultiBinding> 
        </Border.Visibility> 
        <Grid> 
         <Grid.Visibility> 
          <MultiBinding Converter="{StaticResource V_converter }"> 
           <Binding Path="Type"></Binding> 
           <Binding Path="NodeID"></Binding> 
           <Binding Path="TLV"></Binding> 
          </MultiBinding> 
         </Grid.Visibility> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="150"></ColumnDefinition> 
          <ColumnDefinition Width="55"></ColumnDefinition> 
          <ColumnDefinition Width="1*"></ColumnDefinition> 
         </Grid.ColumnDefinitions> 
         <TextBlock x:Name="tbHeader" Text="{Binding Info }" AllowDrop="True" FontWeight="Bold" Grid.Column="2" Visibility="{Binding Visibility_Header}" > 

          <TextBlock.Style> 
           <Style TargetType="TextBlock"> 
            <Setter Property="Background"> 
             <Setter.Value> 
              <!-- this is the default background--> 
              <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
               <GradientStop Color="#FFCEE6C6" Offset="0.008"/> 
               <GradientStop Color="#FF9ECF8C" Offset="0.987"/> 
              </LinearGradientBrush> 
             </Setter.Value> 
            </Setter> 
           </Style> 
          </TextBlock.Style> 
         </TextBlock> 
        </Grid> 
       </Border> 

       <Grid x:Name="GridData"> 

        <Grid.Visibility> 
         <MultiBinding Converter="{StaticResource V_converter }"> 
          <Binding Path="Type"></Binding> 
          <Binding Path="NodeID"></Binding> 
          <Binding Path="TLV"></Binding> 
         </MultiBinding> 
        </Grid.Visibility> 
        <Grid.Background> 
         <MultiBinding Converter="{StaticResource converter }"> 
          <Binding Path="Type"></Binding> 
          <Binding Path="NodeID"></Binding> 
          <Binding Path="TLV"></Binding> 
         </MultiBinding> 
        </Grid.Background> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="150"></ColumnDefinition> 
         <ColumnDefinition Width="55"></ColumnDefinition> 
         <ColumnDefinition Width="1*"></ColumnDefinition> 
        </Grid.ColumnDefinitions> 
        <Border Grid.Column="0" BorderThickness="1" BorderBrush="Black"> 
         <TextBlock Text="{Binding DateTime}" ></TextBlock> 
        </Border> 
        <Border Grid.Column="1" BorderThickness="1" BorderBrush="Black"> 
         <TextBlock Text="{Binding ComPort}"></TextBlock> 
        </Border> 
        <Border Grid.Column="2" BorderThickness="1" BorderBrush="Black"> 
         <TextBlock Text="{Binding Data}" ></TextBlock> 
        </Border> 
       </Grid> 
      </StackPanel> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

回答

1

WPF使得它非常難做到的東西一樣編程,因爲WPF圍繞MVVM模式構建的。因此,要很好地使用它,你需要創建一個ViewModel(來自MVVM的VM)。下面是一個例子視圖模型:

// if you want runtime changes to be reflected in the UI 
public class ItemVM : INotifyPropertyChanged 
{ 
    public string Text { // raise property change in setter } 
    public Color BackgroundColor { // whatever you need } 
} 

然後創建一個這樣的ItemVM列表:

public IEnumerable<ItemVM> Items { get; set; } 

然後在你的XAML綁定到列表和BackgroundColor屬性是這樣的:

<ListView x:Name="MyListView" ItemsSource="{Binding Items}" HorizontalContentAlignment="Stretch"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Text}"> 
      <TextBlock.Background> 
       <SolidColorBrush Color="{Binding BackgroundColor}"/> 
      </TextBlock.Background> 
      </TextBlock> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

如果您需要執行NotifyPropertyChanged的幫助,請按照this文章。

+0

我想爲文本中的某些字符而不是全部文本着色。 –