我在列表视图中有一个文本块,我想在其中动态地为一些字符着色,或者通过某种方式绑定不同文本块的背景颜色。
由于有一些关于角色如何着色的逻辑,我该怎么做?
附加我的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>
答案 0 :(得分:1)
// 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条款。