在WPF DataGrid的XAML中进行条件检查 [英] Conditional checking in xaml of wpf datagrid

查看:253
本文介绍了在WPF DataGrid的XAML中进行条件检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用WPF,由于我是比较传统.net框架的新手,所以我想知道有什么方法可以像在.aspx页中那样检查绑定列的值并基于这些值显示结果.绑定时:

<%#Convert.ToInt32(Eval(UserType))== 1吗? "Admin":用户"%>

我想在WPF数据网格中执行相同的操作.请在下面查看我的代码段:

hi all,

I am using WPF and since I am a newbie to it comparing traditional .net framework, i want to know that is there any way to check the value of binding column and show the results based on those value, as we do in .aspx pages while binding :

<%# Convert.ToInt32(Eval(UserType)) == 1 ? "Admin" : "User" %>

I want to perform the same thing in my WPF datagrid. Please see my code snippet below :

<my:DataGridTemplateColumn Width="112">
                 <my:DataGridTemplateColumn.CellTemplate>
                     <DataTemplate>
                         <StackPanel Orientation="Horizontal" Margin="5,8,5,0">
                             <TextBlock Text="{Binding UserType} "></TextBlock>
                                  </StackPanel>
                     </DataTemplate>
                 </my:DataGridTemplateColumn.CellTemplate>
                 </my:DataGridTemplateColumn




在这里,我得到的UserType为1或2,我希望基于该值以字符串形式显示为"Admin"或"Normal User".任何帮助将不胜感激.

谢谢
Anurag




Here, I am getting UserType as 1 or 2 which I want to show in string as "Admin" or "Normal User" based on the value. Any help would be appreciated.

Thanks
Anurag

推荐答案

在WPF中,一个名为DataTriggers的系统专门用于此目的.您可以按如下方式使用它:

In WPF a system called DataTriggers is entierly, dedicated for this. You can use it like follows:

<TextBlock>
    <TextBlock.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding UserType}" Value="1">
                    <Setter Property="TextBlock.Text" Value="Admin" />
                </DataTrigger>
                <DataTrigger Binding="{Binding UserType}" Value="0">
                    <Setter Property="TextBlock.Text" Value="User" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>


这篇关于在WPF DataGrid的XAML中进行条件检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆