着色某些字符

时间:2017-01-22 09:47:41

标签: c# wpf 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 个答案:

答案 0 :(得分:1)

由于WPF是围绕MVVM模式构建的,因此WPF使得以编程方式执行此类操作非常困难。因此,要很好地使用它,您需要创建一个ViewModel(来自MVVM的VM)。这是一个示例ViewModel:

// 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条款。