简体   繁体   English

如何使用绑定更改datagrid textblock列的前景

[英]how to change the foreground of the datagrid textblock column using binding

In my application, I have used "DataGrid" to bind set of values dynamically. 在我的应用程序中,我使用“DataGrid”动态绑定值集。 Also based upon the values binded, I need to change the forecolor of the values binded. 同样基于绑定的值,我需要更改绑定值的前景色。

Here is my XAML and C# code 这是我的XAML和C#代码

XAML: XAML:



                            <my:DataGridTemplateColumn Header="Priority">
                                <my:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock HorizontalAlignment="Left" Name="txtPriority" Text="{Binding MessagePriority}" FontWeight="Bold">  
                                          <TextBlock.Resources>
                                            <Style TargetType="TextBlock">
                                                <Setter Property="Foreground" Value="{Binding ForegroundBrush}" />
                                            </Style>
                                        </TextBlock.Resources>
                                        </TextBlock>

                        </my:DataGrid.Columns>                            

                    </my:DataGrid>

C#: C#:


private SolidColorBrush _foregroundBrush;

        public SolidColorBrush ForegroundBrush
        { 
            get
            {
                return _foregroundBrush;
            }
            set
            {
                if (_foregroundBrush != value)
                {
                    _foregroundBrush = value;
                    RaisePropertyChanged(() => _foregroundBrush);
                }
            }
        }

var color = (Color)ColorConverter.ConvertFromString("#FF00FF");
                                    var brush = new SolidColorBrush(color);
                                    ForegroundBrush = brush;  

There are a couple of ways that you can do this. 有几种方法可以做到这一点。 It very much depends on your requirements, so it would have been better if you had explained them better and in more detail. 这在很大程度上取决于您的要求,所以如果您更好地和更详细地解释它们会更好。 However as you didn't, I can't use your exact details in this example and you will have to adapt it to your project. 但是,由于您没有,我不能在此示例中使用您的确切详细信息,您将不得不使其适应您的项目。 The first way is to simply use a DataTrigger ... this method is good for up to around 8 different value/colour pairs: 第一种方法是简单地使用DataTrigger ......这种方法适用于最多8种不同的值/颜色对:

<DataGrid ItemsSource="{Binding RadioButtonData}">
    <DataGrid.Columns>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Label}">
                        <TextBlock.Style>
                            <Style>
        <Style.Triggers>
            <DataTrigger Binding="{Binding SomeValue}" Value="Something">
                <Setter Property="TextElement.Foreground" Value="LightGreen" />
            </DataTrigger>
            <DataTrigger Binding="{Binding SomeValue}" Value="Another thing">
                <Setter Property="TextElement.Foreground" Value="LightBlue" />
            </DataTrigger>
            <DataTrigger Binding="{Binding SomeValue}" Value="A different thing">
                <Setter Property="TextElement.Foreground" Value="LightPink" />
            </DataTrigger>
        </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

The other method that you can use is to use a Converter class with your Binding . 您可以使用的另一种方法是将Converter类与Binding You can find a detailed example of doing this in many online posts... here are a few: 您可以在许多在线帖子中找到这样做的详细示例......以下是一些:

How to set Foreground of DataGrid Column with ValueConverter 如何使用ValueConverter设置DataGrid列的前景
Datagrid AutoGenerateColumns="True" forecolor IValueConverter Datagrid AutoGenerateColumns =“True”forecolor IValueConverter

Hai Everyone, Hai Everyone,

        First of all i thanks to all for see my question and at that time send your answers. Now, i got the answer, i have to use the converter class for convert color to Solidcolorbursh like as below:

ValueConverter.cs ValueConverter.cs

 public class ValueConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || !(value is decimal))
                return new SolidColorBrush(ForegroundBrush);

            var dValue = System.Convert.ToDecimal(value);
            if (dValue < 0)
                return new SolidColorBrush(ForegroundBrush);
            else
                return new SolidColorBrush(ForegroundBrush);
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }

    }    

XMAL: XMAL:

First we can decalre that class namespace like: 首先,我们可以对该类命名空间进行decalre,如:

xmlns:convert="clr-namespace:Helper"
<Grid.Resources>
        <convert:ValueConverter x:Key="ValueConverter"></convert:ValueConverter>
 </Grid.Resources>

<my:DataGridTemplateColumn Header="Priority">
        <my:DataGridTemplateColumn.CellTemplate>
               <DataTemplate>
               <TextBlock HorizontalAlignment="Left" Name="txtPriority" Text="{Binding Priority}" FontWeight="DemiBold" Foreground="{Binding Priority, Converter={StaticResource ValueConverter}}">
 </TextBlock>

                  </DataTemplate>
            </my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM