Skip to main content
 首页 » 编程设计

c#之如何使用绑定(bind)更改数据网格文本 block 列的前景

2024年02月13日14thcjp

在我的应用程序中,我使用“DataGrid”来动态绑定(bind)一组值。同样基于绑定(bind)的值,我需要更改绑定(bind)值的前景色。

这是我的 XAML 和 C# 代码

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#:


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;   

请您参考如下方法:

有几种方法可以做到这一点。这在很大程度上取决于您的要求,所以如果您能更好、更详细地解释它们会更好。但是,正如您没有做的那样,我无法在此示例中使用您的确切详细信息,您将不得不根据您的项目进行调整。第一种方法是简单地使用 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> 

您可以使用的另一种方法是将Converter 类与您的Binding 一起使用。您可以在许多在线帖子中找到执行此操作的详细示例...以下是一些:

How to set Foreground of DataGrid Column with ValueConverter
Datagrid AutoGenerateColumns="True" forecolor IValueConverter