WPF DataGrid中对DataGridTextColumn中Style的属性设置Binding

林夕合鸟 2015-12-23 03:52:53
有点绕,直接上代码:

<DataGrid x:Name="dgMain" CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="+0" Binding="{Binding Value0}" Width="*">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding Value0}"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>


我想在C#代码中实现以上内容,如何实现?谢谢了!在线急等!
关键在加红部分代码,其他我已经实现:
代码如下:
SingleData d = new SingleData();
d.Value0 = 5;
d.Value1 = 10;

list.Add(d);

DataGridTextColumn column = new DataGridTextColumn();
column.Header = "+0";
Binding bind = new Binding();

bind.Path = new PropertyPath("Value0");

column.Binding = bind;


dgMain.Columns.Add(column);

dgMain.ItemsSource = list;
...全文
902 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
林夕合鸟 2015-12-23
  • 打赏
  • 举报
回复
搞定了。留个代码帮助后面的人。

            DataGridTemplateColumn column = new DataGridTemplateColumn();
            column.Header = "+1";
            column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);

            DataTemplate dt = new DataTemplate();
            
            FrameworkElementFactory fef = new FrameworkElementFactory(typeof(TextBlock));

            Binding bind = new Binding("Value1");
            fef.SetBinding(TextBlock.TextProperty, bind);
            //**************
            Binding bindColor = new Binding("Value1");
            bindColor.Converter = new BackgroundValueToColor();
            fef.SetBinding(TextBlock.BackgroundProperty, bindColor);

            dt.VisualTree = fef;

            column.CellTemplate = dt;

            dgMain.Columns.Add(column);
            dgMain.ItemsSource = list;

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧