从列表视图动态获取textBlock

时间:2017-01-18 09:57:20

标签: c# wpf xaml listview textblock

我有一个ListView,它包含2个textBlocks作为listview Item

我想动态地为textBlocks着色

我该如何处理文本块? 将会有一个例子。

感谢您的支持

附加我的XAML:

<ListView x:Name="LV" ItemsSource= "{Binding  Lggv}" SelectionChanged="dataGridData_SelectionChanged" ItemContainerStyle="{StaticResource ListViewItemStyle}" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel >
                            <Border BorderThickness="1" BorderBrush="Black">

                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="150"></ColumnDefinition>
                                        <ColumnDefinition Width="50"></ColumnDefinition>
                                        <ColumnDefinition Width="1*"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock x:Name="tbHeader" Text="{Binding Info }" AllowDrop="True"   FontWeight="Bold" Grid.Column="2" >
                                    <TextBlock.Background>
                                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">                                              
                                            <GradientStop Color="#FFCEE6C6" Offset="0.008"/>
                                            <GradientStop Color="#FF9ECF8C" Offset="0.987"/>
                                        </LinearGradientBrush>
                                    </TextBlock.Background>

                                </TextBlock>
                                </Grid>
                            </Border>
                            <Grid x:Name="GridData" >
                                <Grid.Background>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FFC5DDFF" Offset="0"/>
                                        <GradientStop Color="#FFA8C8F7" Offset="1"/>
                                    </LinearGradientBrush>
                                </Grid.Background>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="150"></ColumnDefinition>
                                    <ColumnDefinition Width="50"></ColumnDefinition>
                                    <ColumnDefinition Width="1*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Border Grid.Column="0" BorderThickness="2" BorderBrush="Black">
                                    <TextBlock Text="{Binding DateTime}" ></TextBlock>
                                </Border>
                                <Border Grid.Column="1" BorderThickness="2" BorderBrush="Black">
                                    <TextBlock Text="{Binding ComPort}"></TextBlock>
                                </Border>
                                <Border Grid.Column="2" BorderThickness="2" BorderBrush="Black">
                                    <TextBlock Text="{Binding Data}" ></TextBlock>
                                </Border>
                            </Grid>
                        </StackPanel>
                    </DataTemplate>    
                </ListView.ItemTemplate>                                

            </ListView>

5 个答案:

答案 0 :(得分:1)

执行此操作的最佳方法是使用IValueConverter

因为这可以让你保持你的视图和viewModel分开,不过如果你不关心@sasanaf答案是完全正确的

这是一个简单的基于画笔的转换器

的示例
public class PriorityConverter : IValueConverter
{
    public Brush HighBrush { get; set; }
    public Brush LowBrush { get; set; }
    public Brush MediumBrush { get; set; }
    public Brush DefaultBrush { get; set; }
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var priority = value as Priority?;
        if (priority.HasValue)
        {
            switch (priority.Value)
            {
                case Priority.High:
                    return HighBrush;
                case Priority.Medium:
                    return MediumBrush;
                case Priority.Low:
                    return LowBrush;
                default:
                    return DefaultBrush;
            }
        }
        else
            throw new InvalidCastException($"{value} is not a Priority");
    }

然后,您可以将转换器作为资源添加到您的应用程序

<local:PriorityConverter x:Key="PriorityConverter" >
    <local:PriorityConverter.DefaultBrush>
        <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlColorKey}}"/>
    </local:PriorityConverter.DefaultBrush>
    <local:PriorityConverter.HighBrush>
        <LinearGradientBrush>
            <GradientStop Color="Red" Offset="0.5"/>
            <GradientStop Color="Yellow" />
        </LinearGradientBrush>
    </local:PriorityConverter.HighBrush>
    <local:PriorityConverter.MediumBrush>
        <SolidColorBrush Color="Blue"/>
    </local:PriorityConverter.MediumBrush>
    <local:PriorityConverter.LowBrush>
        <SolidColorBrush Color="Green"/>
    </local:PriorityConverter.LowBrush>
</local:PriorityConverter>

最后在绑定上使用它

<TextBlock Background="{Binding Priority, Converter={StaticResource PriorityConverter}}" />

答案 1 :(得分:0)

如果您正在使用MVVM方法,我会在ViewModel中设置颜色并将它们绑定到属性。

编辑: 像这样的文本块:

                               <TextBlock DataContext="ViewModel"
                                       Background="{Binding SpecificColor}">
                               </TextBlock>

在ViewModel中,您可以在初始化时或在任何特定事件中指定您想要的颜色。 例如:

private Brush m_SpecificColor;
    public Brush SpecificColor
    {
        get { return m_SpecificColor; }
        set
        {
            m_SpecificColor = value;
            OnPropertyChanged("SpecificColor");
        }
    }
    private void SetColor()
    {
        SpecificColor = (Brush)new BrushConverter().ConvertFromString("Green");
    }

答案 2 :(得分:0)

我想您希望根据Lggv源集合中对象的某些属性(具有“Info”,“DateTime”,“ComPort”和“Data”属性的类)动态更改颜色。

然后你可以使用一个带有DataTrigger的样式来绑定到这个类的属性,即类型T(同样这是带有“Info”,“DateTime”,“ComPort”的类型和您绑定到的IEnumerable<T>(Lggv)源集合的“数据”属性:

<TextBlock x:Name="tbHeader" Text="{Binding Info}" AllowDrop="True"   FontWeight="Bold" Grid.Column="2">
    <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.Triggers>
                <!-- This trigger changes the background to green when the "Info" property of your data object returns "Some info..." -->
                <DataTrigger Binding="{Binding Info}" Value="Some info...">
                    <Setter Property="Background">
                        <Setter.Value>
                            <SolidColorBrush>Green</SolidColorBrush>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

答案 3 :(得分:0)

您可以使用IValueConverter将属性转换为背景色。以下是一个例子。

else

在资源中声明值转换如下。

class BackgroundColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush();
        if (value != null )
        {
            string strValue = value.ToString();


            myLinearGradientBrush.StartPoint = new System.Windows.Point(0, 0);
            myLinearGradientBrush.EndPoint = new System.Windows.Point(1, 1);

            switch (strValue)
            {
                case "Match1":
                    myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));
                    myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.Red, 0.25));
                    break;
                case "Match2":
                    myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.Blue , 0.0));
                    myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.BlueViolet, 0.25));
                    break;
                default:
                    myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.Green , 0.0));
                    myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.GreenYellow, 0.25));
                    break;
            }
        }
        return myLinearGradientBrush;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

以下是Xaml的编码器。

<Window.Resources>
    <local:BackgroundColorConverter x:Key="BackgroundColorConverter"/>
 </Window.Resources>

另一种方法,如果你想使用资源。您可以在Windows或用户控制资源中定义颜色。在此之后,您将不得不使用IMultiValueConverter。将属性和窗口或用户控件传递给MultiValueConverter。您需要使用相对源来获取绑定中的windows或usercontrol。然后,您可以根据匹配条件返回资源。

答案 4 :(得分:0)

我个人用DataTrigger来做这件事:

<Style x:Key="TextBlockStyle" TargetType="TextBlock">
    <Setter Property="Background">
        <Setter.Value>
            <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.Triggers>
        <DataTrigger Binding="{Binding YourProperty}" Value="YourPropertyValue">
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Red" Offset="0.008"/>
                        <GradientStop Color="Orange" Offset="0.987"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

然后你不必定义一个类来做一个相对简单的任务。