首页 新闻 会员 周边

WPF DataGrid 根据字段值的不同,显示不同的图片(或文字)

0
悬赏园豆:80 [已解决问题] 解决于 2018-09-21 10:09

如题,Degree字段是Double类型,我想达到这种效果:如果Degree值为1,XAML中的image中显示“完成”状态的图片;如果Degree值为0,XAML中的image中显示“待处理”状态的图片;如果Degree值不为0也不为1,XAML中的image中显示“处理中”状态的图片;

本人小白,刚刚学WPF,望大佬们指点

          <!--#region 状态-->
                <DataGridTemplateColumn Header="状态" Width="70">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
                                <CheckBox Width="16" Height="16" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Margin="0,5,0,0"></CheckBox>
                                <Image Source="{Binding Degree}" Width="16" Height="16" VerticalAlignment="Center" HorizontalAlignment="Center"></Image>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <!--#endregion-->

紫晶城的主页 紫晶城 | 初学一级 | 园豆:-4
提问于:2018-08-07 17:19
< >
分享
最佳答案
0

重写DataGrid,重写创建Item的函数,在context中判断,来指导Item(可以自己派生DataGridColumn基类)的创建或者属性即可。

收获园豆:70
花飘水流兮 | 专家六级 |园豆:13560 | 2018-08-07 17:49

貌似你说的很清楚,我不是太懂。e.....尴尬

紫晶城 | 园豆:-4 (初学一级) | 2018-08-07 17:52

@紫晶城: public class DataGridEx:DataGrid

{

 重写函数,自己去看

}

花飘水流兮 | 园豆:13560 (专家六级) | 2018-08-07 17:53

@花飘水流兮: Ok,谢谢

紫晶城 | 园豆:-4 (初学一级) | 2018-08-07 17:54

@紫晶城: 核心还是DataGridColumn这个哈。

把重写的DataGridColumn这个 放到DataGridEx中,当然不重写Grid也可以。

DataGridColumn是要点,因为他负责创建行。

花飘水流兮 | 园豆:13560 (专家六级) | 2018-08-07 18:02

@花飘水流兮: 好的,我研究下

紫晶城 | 园豆:-4 (初学一级) | 2018-08-07 18:04

@紫晶城: 最后告诫——少用xmal绑定,用得越多越惨。不仅如此,即使对象变了也不好去改。

我虽然也就用了两三次,给你个示例:

较好的方式:

        public 自动发言Panel(自动发言 config)
        {
            InitializeComponent();
            DataContext = config;

            _c时间间隔.SetBinding(IntegerUpDown.ValueProperty, ClassEx.GetPropertyName<自动发言>(t => t.周期时长));
            _cIsRandom.SetBinding(ToggleButton.IsCheckedProperty, ClassEx.GetPropertyName<自动发言>(t => t.IsRandom));

            _cListbox.ItemsSource = config.内容;
            _cListbox.OnGenerateItem = OnGenerateItem;

            _cAdder.Click += CAdderOnClick;
            //_cRickLevel.Text = $"{config.等级}";
        }

你态度很好,实现代码一并给你了:

    public class ColumnEx:DataGridColumn
    {
        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            //这里随便创建UiElement,这就是你想要的,dataItem就是你的数据Entity类
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            throw new NotImplementedException();
        }
    }

你不编辑,下面函数那个可以不管。

花飘水流兮 | 园豆:13560 (专家六级) | 2018-08-07 18:15
其他回答(1)
4

我觉得你定义一个converter就可以搞定了,在convert里根据不同的值返回不同的image source。

<Image Source="{Binding Degree, convert={staticresource XXXX}}" Width="16" Height="16" VerticalAlignment="Center" HorizontalAlignment="Center"> </Image>

收获园豆:10
会长 | 园豆:12401 (专家六级) | 2018-08-08 17:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册