DatagridTemplateColumn with triggers failed to bind - wpf

I have a datagrid that contain of the 2 columns and I would like to show some of cells in column#2 to be:
ComboBox
TextBox
based on property
code :
Solution #1 :
<Window.Resources>
<DataTemplate x:Key="DropDownTemplate">
<StackPanel>
<ComboBox SelectedValuePath="Id" DisplayMemberPath="Name" ItemsSource="{Binding MarketConfigurationLOVs, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="TextBoxTemplate">
<StackPanel>
<TextBox Text="{Binding ConfigurationValue, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
Here the datagrid tags :
<DataGrid ItemsSource="{Binding Path= MarketConfigurationValues,Mode=TwoWay}" HeadersVisibility="None" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Width="120" Binding="{Binding Path= ConfigurationName}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl Content="{Binding}" >
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding HasLOV}" Value="true">
<Setter Property="ContentTemplate" Value="{StaticResource DropDownTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding HasLOV}" Value="false">
<Setter Property="ContentTemplate" Value="{StaticResource TextBoxTemplate}" />
</DataTrigger>
<!-- and so on -->
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
this failed to show actual value as it show the name of property and always show cell in dropdown and when I swapped the TextBoxContentTemplate with ComboBoxContentTemplate it show all cells as textBox so it seem it ignore the trigger however when I debug I found the HasLOV some items contain true and some contain false
Solution #2 : (also failed) Reference : Jon solution from the following post WPF MVVM Creating Dynamic controls
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl x:Name="MyContentControl" Content="{Binding}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding HasLOV}" Value="false">
<Setter TargetName="MyContentControl" Property="ContentTemplate" Value="{StaticResource TextBoxTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding HasLOV}" Value="true">
<Setter TargetName="MyContentControl" Property="ContentTemplate" Value="{StaticResource DropDownTemplate}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
ViewModel is big class but I pulled here the main parts related to my problem . In constructor I created list of MarketConfigurationValue and I filled values inside MarketConfigurationLOVs in case if I set HasLov=true and I filled value in configurationValue in case if I set HasLov=false and I run the application and in debug mode I found the main object List contain what I said correctly but it failed to show comboBox in case HasLov=true and textBox in case HasLov=false :
public class MarketConfigurationValue
{
public string ConfigurationName { get; set; }
public string ConfigurationValue { get; set; }
public int ConfigurationValueId { get; set; }
public List<Market_Config_Lov> MarketConfigurationLOVs {get;set;}
public bool HasLov { get; set; }
}
public class Market_Config_Lov
{
public virtual string Name { get; set; }
public virtual int Sequence { get; set; }
}
Can you please help ?

Here's how you implement the INotifyPropertyChanged Interface. With this the binding updates itself when the setter of HasLOV is called.
public class MarketConfigurationValue:INotifyPropertyChanged
{
private bool _hasLOV;
public bool HasLOV
{
get {return _hasLOV;}
set {_hasLOV = value; OnPropertyChanged("HasLOV");}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
VerifyPropertyName(propertyName);
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
}
Hope this helps.
====>The property name is typo as the property in class called : HasLov and in XAML called HasLOV .

Related

TabControl view doesn't update on template change

I need to change the view of a TabControl's content on-the-fly.
I am guessing the best way to accomplish this is to define the view as a DataTemplate, and then change said template using a trigger.
In my test app, the background color is tied to the same data trigger as the template. The background color updates immediately upon making the radio button selection.
Expected behavior: The Tab Item Content / DataTemplate also updates immediately.
Actual Behavior: Tab content view does not update until the tab selection is changed.
Here's my Minimal, Complete, and Verifiable example:
Window XAML
<Window x:Class="ChangeView.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Title="Window1" Height="350" Width="400">
<Window.Resources>
<DataTemplate x:Key="ContentTemplate1">
<Grid>
<Label HorizontalAlignment="Center" VerticalAlignment="Center" Content="{Binding MyBlurb}"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="ContentTemplate2">
<Grid>
<Label HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
Content="{Binding MyHeader}" Background="Black" Foreground="White" FontSize="72"/>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Style.Triggers>
<DataTrigger Binding="{Binding ViewType1}" Value="False">
<Setter Property="Background" Value="Chartreuse"/>
</DataTrigger>
<DataTrigger Binding="{Binding ViewType1}" Value="True">
<Setter Property="Background" Value="Bisque"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Margin="10,38,0,0" Text="Content Template:"/>
<RadioButton x:Name="radio1" Margin="120,40,0,0" Grid.ColumnSpan="2" Content="1" GroupName="ViewSelect" IsChecked="{Binding Path=ViewType1}"/>
<RadioButton Margin="170,40,0,0" Grid.ColumnSpan="2" Content="2" GroupName="ViewSelect"/>
<TabControl Grid.Row="1" ItemsSource="{Binding TabGroup}">
<TabControl.Style>
<Style TargetType="{x:Type TabControl}">
<Setter Property="Margin" Value="10"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ViewType1}" Value="True">
<Setter Property="ContentTemplate" Value="{DynamicResource ContentTemplate1}"/>
</DataTrigger>
<DataTrigger Binding="{Binding ViewType1}" Value="False">
<Setter Property="ContentTemplate" Value="{DynamicResource ContentTemplate2}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TabControl.Style>
<TabControl.ItemTemplate>
<DataTemplate>
<Border x:Name="headerBorder">
<Label Content="{Binding MyHeader}" FontSize="20"/>
</Border>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
</Grid>
</Window>
Code Behind
namespace ChangeView
{
using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window, INotifyPropertyChanged
{
public ObservableCollection<TabData> TabGroup { get; set; } = new ObservableCollection<TabData>();
private bool _viewType1 = true;
public bool ViewType1
{
get { return _viewType1; }
set { _viewType1 = value; RaisePropertyChanged(nameof(ViewType1)); }
}
public Window1()
{
TabGroup.Add(new TabData("♻️", "Recycle"));
TabGroup.Add(new TabData("⚔", "Swords"));
TabGroup.Add(new TabData("⚗", "Chemistry"));
TabGroup.Add(new TabData("🌵", "Cactus"));
TabGroup.Add(new TabData("👺", "Tengu"));
TabGroup.Add(new TabData("🐙", "Octopus"));
DataContext = this;
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void RaisePropertyChanged(string propName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
public class TabData : INotifyPropertyChanged
{
private string _myHeader, _myBlurb;
public TabData(string header, string blurb)
{
MyHeader = header;
MyBlurb = blurb;
}
public string MyHeader
{
get { return _myHeader; }
set { _myHeader = value; RaisePropertyChanged(nameof(MyHeader)); }
}
public string MyBlurb
{
get { return _myBlurb; }
set { _myBlurb = value; RaisePropertyChanged(nameof(MyBlurb)); }
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void RaisePropertyChanged(string propName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
After changing the radio button state, change the selected tab. You will then see the correct content template.
It looks as if, in a TabControl, changing the content template alone does not cause the content to be rendered. If you render new content by switching the selected tab, the current content template will then be used.
So let's write one ContentTemplate, which creates a ContentControl and switches the ContentControl's ContentTemplate. I've tested, and the ContentControl will re-render its content when its ContentTemplate changes. The bindings get a little bit verbose.
<TabControl ItemsSource="{Binding TabGroup}" Grid.Row="1">
<TabControl.ContentTemplate>
<DataTemplate>
<ContentControl
x:Name="ContentCtl"
Content="{Binding}"
/>
<DataTemplate.Triggers>
<DataTrigger
Binding="{Binding DataContext.ViewType1, RelativeSource={RelativeSource AncestorType=TabControl}}"
Value="True">
<Setter
TargetName="ContentCtl"
Property="ContentTemplate"
Value="{DynamicResource ContentTemplate1}"
/>
</DataTrigger>
<DataTrigger
Binding="{Binding DataContext.ViewType1, RelativeSource={RelativeSource AncestorType=TabControl}}"
Value="False"
>
<Setter
TargetName="ContentCtl"
Property="ContentTemplate"
Value="{DynamicResource ContentTemplate2}"
/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</TabControl.ContentTemplate>
<TabControl.ItemTemplate>
<DataTemplate>
<Border x:Name="headerBorder">
<Label Content="{Binding MyHeader}" FontSize="20"/>
</Border>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
You could also do something ugly in your code behind to make the TabControl render itself again on command. Or maybe you can replace the metadata on TabControl.ContentTemplate.

Content control, ContentTemplateSelector, how to choose a staticresource based on value comming through binding, without datatriggers

In a resource dictionary, I have few Viewboxes, and a datatemplate which has content control in it. I’m using style to switch between the viewboxes like this
<DataTemplate x:Key="foo">
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding Viewbox}" Value="SageCashYes">
<Setter Property="Content" Value="{StaticResource SageCashYesViewbox}" />
</DataTrigger>
<DataTrigger Binding="{Binding Viewbox}" Value="SageCashNo">
<Setter Property="Content" Value="{StaticResource SageCashNoViewbox}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
Is there anyway to do something like this
<DataTemplate x:Key="foo">
<ContentControl ContentTemplateSelector="{Binding Viewbox}" >
</DataTemplate>
So I’d like to be able to tell it to choose appropriate Viewbox based on this binding but without these data triggers?
I’m asking it because the view boxes have vectors inside to draw images, and these data trigger will grow huge in no time. So if there is something out there which will make my life easier I’d like to know about it.
EDIT
<ListBox ItemTemplate="{StaticResource Foo}
ItemsSource="{Binding MyList}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
public class MyViewModel
{
public MyViewModel()
{
MyList = new List<MyList>()
{
new MyItem() {BoolValue = false, Heading = "Bank Payment", Viewbox = "SageCashNo"},
new MyItem() {BoolValue = true, Heading = "Cash Payment", Viewbox = "SageCashYes"}
};
}
public List<MyItem> MyList { get; set; }
}
public class MyItem
{
public bool {BoolValue { get; set; }
public string Heading { get; set; }
public string Viewbox { get; set; }
}
Kind Regards
Daniel

Bind to HierarchicalDataTemplate DataContext from nested DataTemplate

I have my treeview with 2 level nested levels.
<TreeView ItemsSource="{Binding Project.Tables}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type TableModel}" ItemsSource="{Binding Columns}">
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type ColumnModel}">
</DataTemplate>
</TreeView.Resources>
</TreeView>
My binding source with tables:
public class ProjectModel : PropertyChangedBase
{
private BindableCollection<TableModel> _tables;
public BindableCollection<TableModel> Tables
{
get { return _tables; }
set
{
if (_tables != value)
{
_tables = value;
NotifyOfPropertyChange();
}
}
}
}
public class TableModel
{
public BindableCollection<ColumnModel> Columns { get; set; }
public bool IsSelected
{
get;
}
}
I tried to use next binding:
<DataTemplate DataType="{x:Type projectModels:ColumnModel}">
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=DataContext.IsSelected }" Value="True">
<Setter Property="Foreground" Value="{StaticResource GreyBrush3}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBlock Text="{Binding ColumnName}">
</TextBlock>
</StackPanel>
</DataTemplate>
Inside DataTemplate I want to have binding to DataContext of HierarchicalDataTemplate. I already tried different AncestorLevels and it did not work, any ideas how can I do it?

How to do binding in WPF - C#

I have following code:
<ControlTemplate x:Key="ViewItemTemplate"
TargetType="ListViewItem">
<StackPanel Orientation="Horizontal">
<CheckBox Margin="0,0,3,0" x:Name="CkBox">
<CkBox.IsChecked>
<Binding Path="IsSelected"
Mode="TwoWay">
<Binding.RelativeSource>
<RelativeSource Mode="TemplatedParent" />
</Binding.RelativeSource>
</Binding>
</CkBox.IsChecked>
<DataTrigger Binding="{Binding InvalidForeground}" Value="true">
<Setter TargetName="CkBoxVisual" Property="Foreground" Value="#999999"/>
</DataTrigger>
</CheckBox>
<ContentPresenter />
</StackPanel>
</ControlTemplate>
How can i bind InvalidForeground? I looked online for many example they tell to use DataTemplate. But when i add DataTemplate above StackPanel i get errors? Am i doing something wrong?
I am trying to bind InvalidForeground so i can add some code to it. I am getting an error: Cannot resolve symbol 'InvalidForeground' due to unknown DataContext.
It appears you are trying to declare a customized checkbox control to use within WPF application. As such, your "InvalidForeground" property would be exposed, but the template doesn't understand what its real type is expected.
I've posted another answer here which gave a full step-by-step for a custom button. The principles are the same, and I've tried to point out my understanding of of the declarations, the type, etc. Hopefully it can guide you on not just this, but other class templates too.
<ControlTemplate x:Key="ViewItemTemplate"
TargetType="ListViewItem">
<StackPanel Orientation="Horizontal">
<CheckBox Margin="0,0,3,0" x:Name="CkBox">
<CkBox.IsChecked>
<Binding Path="IsSelected"
Mode="TwoWay">
<Binding.RelativeSource>
<RelativeSource Mode="TemplatedParent" />
</Binding.RelativeSource>
</Binding>
</CkBox.IsChecked>
<DataTrigger Binding="{Binding InvalidForeground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" Value="true">
<Setter TargetName="CkBoxVisual" Property="Foreground" Value="#999999"/>
</DataTrigger>
</CheckBox>
<ContentPresenter />
</StackPanel>
</ControlTemplate>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
abc A = new abc();
A.InvalidForeground = true;
}
}
public class abc : INotifyPropertyChanged
{
private bool invalidForeGround;
public bool InvalidForeground
{
get
{ return invalidForeGround; }
set
{
invalidForeGround = value;
Notify("InvalidForeground");
}
}
private void Notify(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
InvalidForeground must be the property in the DataContext of the Control whose template the above code is.I hope this will help

WPF Change ViewModel Property in View

I have a WPF DataGrid with several Templated Columns containing Checkboxes.
If I press a specific Checkbox the source of another checkbox has to be set.
I have a Viewmodel with a Property called Property (yes i know :)), and this Property has the Property "Visible".
If I check the "Mandatory" checkbox I want the "Visible" Value to be set and so the visible Checkbox to be set too, but somehow this is not working.
Heres my code:
<toolkit:DataGridTemplateColumn Header="Mandatory" IsReadOnly="False">
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<CheckBox IsChecked="{Binding Path=Mandatory,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=MandatoryDB}" Value="True">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=MandatoryDB}" Value="False">
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<ei:ChangePropertyAction TargetObject="{Binding UpdateSourceTrigger=PropertyChanged}" PropertyName="ReadOnly" Value="False" />
<ei:ChangePropertyAction TargetObject="{Binding NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" PropertyName="Visible" Value="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</StackPanel>
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
Thanks,
Jonny
I think what you're trying to do is to drive your business logic by your View which is a horrible anti-pattern. What you need to is when the value of the "Mandatory" checkbox is changed then your viewmodel needs to set other properties accordingly and alert your view that there was a change (INotifyPropertyChanged).
Your view should not set any viewmodel properties, it should only read them and pass user input, the only other things which your view needs to controll are converters and other view-only items.
Hope this helps
see if this can help you.
lets assume that you want to show yes, no and ans in your Datagrid. for this find the sample code below.
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Coll}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Yes">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Width="50"
Height="50"
IsChecked="{Binding Yes,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="No">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Width="50"
Height="50"
IsChecked="{Binding No,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Ans">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Rectangle Width="100"
Height="50"
RadiusX="25"
RadiusY="25">
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Style.Triggers>
<DataTrigger Binding="{Binding Ans}" Value="true">
<Setter Property="Fill" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Ans}" Value="false">
<Setter Property="Fill" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
code behind
public partial class YesNoTest : Window
{
public YesNoTest()
{
InitializeComponent();
if (Coll == null)
{
Coll = new ObservableCollection<Anss>();
for (int index = 0; index < 10; index++)
{
Coll.Add(new Anss() { Yes = false, No = false, Ans = true });
}
}
this.DataContext = this;
}
private ObservableCollection<Anss> _coll;
public ObservableCollection<Anss> Coll
{
get { return _coll; }
set { _coll = value; }
}
}
public class Anss : INotifyPropertyChanged
{
private bool _Yes;
public bool Yes
{
get
{
return _Yes;
}
set { _Yes = value; OnChanged("Yes"); OnChanged("Ans"); }
}
private bool _No;
public bool No
{
get
{
if (_No == true)
Yes = false;
return _No;
}
set { _No = value; OnChanged("No"); OnChanged("Ans"); }
}
private bool _Ans;
public bool Ans
{
get
{
if (Yes == true)
_Ans = true;
else
_Ans = false;
return _Ans;
}
set { _Ans = value; OnChanged("Ans"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}

Resources