Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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 :

XML
<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



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
Posted

1 solution

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

XML
<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>
 
Share this answer
 
Comments
@nuraGGupta@ 5-Oct-10 23:54pm    
Thanks pumba......it helped me !!!
Member 9460719 3-Jun-19 5:20am    
Thanks...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900