GridViewColumn with ComboBox and UserControl - c#

I have a GridViewColumn with Combobox BAsed on ComboBox selected I want to dynamically put UI elements.Below given is the code.OperatorList has three values "Between","After","Before".Based on this selection the template has to be loaded.If "Between" Multiple TextBox else Single textbox.This part works fine.But to get the content of this and store in Model Class for each row is where i am encountering problem.
<StackPanel>
<ComboBox Grid.Column="0" ItemsSource="{Binding Path=OperatorList}" IsEditable="True"
IsSynchronizedWithCurrentItem="True"
SelectedValue="{Binding ReferenceOperatorSelected}" />
<UserControl x:Name="MyControl">
</UserControl>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding ReferenceOperatorSelected}" Value="Between">
<Setter TargetName="MyControl" Property="ContentTemplate" Value="{StaticResource MultipleTextBoxTemplate}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding ReferenceOperatorSelected}" Value="After">
<Setter TargetName="MyControl" Property="ContentTemplate" Value="{StaticResource SingleTextBoxTemplate}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding ReferenceOperatorSelected}" Value="Before">
<Setter TargetName="MyControl" Property="ContentTemplate" Value="{StaticResource SingleTextBoxTemplate}"></Setter>
</DataTrigger>
</DataTemplate.Triggers>
<DataTemplate x:Key="MultipleTextBoxTemplate">
<StackPanel>
<TextBox Text="{Binding Path=BetweenValue1,Mode=TwoWay,NotifyOnSourceUpdated=True}" MinWidth="40"></TextBox>
<TextBox Text="{Binding Path=BetweenValue2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" MinWidth="40"></TextBox>
</StackPanel>
</DataTemplate>
In my Model class I have two properties BetweenValue1 and BetweenVAlue2 ...These are not updated when i key in value in textboxes..

You have not set the Content for your UserControl. just try doing
<UserControl x:Name="MyControl" Content="{Binding}"/>

Related

How to change background of the template item, on selecting DataGridCell

I need to change the background color of a Textbox that is inside DataGridTemplateColumn cell based on whether or not if the data cell is selected.
Currently what I managed to do is, changing the background color of the template cell, which became useless because of error management.
Thus, I want to change the background of the Textbox, and revert it back on selection changed.
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="DarkBlue"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn IsReadOnly="True" MinWidth="150" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=TwoDLineName, Mode=TwoWay,UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
HorizontalAlignment="Stretch" Style="{StaticResource TextBoxValidated}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
You could use a Style with a DataTrigger that binds to the IsSelected property of the parent row:
<DataGridTemplateColumn IsReadOnly="True" MinWidth="150" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=TwoDLineName, Mode=TwoWay,UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True, NotifyOnValidationError=True}">
<TextBox.Style>
<Style TargetType="TextBox" BasedOn="{StaticResource TextBoxValidated}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected,
RelativeSource={RelativeSource AncestorType=DataGridRow}}"
Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

DataTrigger with Property = ItemControl.ItemTemplate

I Am having some trouble getting this binding to work. I have several objects within SettingCollection that all have an enum property. I want to generate a control based on what the value of this is. But when I check this value with a data trigger it does not work.
Can anyone provide some insight into how i can accomplish this?
<Window.Resources>
<DataTemplate x:Key="CheckboxNode">
<CheckBox IsChecked="{Binding Status}" Margin="0,5,0,0">
<ContentPresenter Content="{Binding DisplayName}"/>
</CheckBox>
</DataTemplate>
<DataTemplate x:Key="TextboxNode">
<TextBox Text="Badgers"></TextBox>
</DataTemplate>
</Window.Resources>
<ItemsControl ItemsSource="{Binding SettingCollection}">
<ItemsControl.Style>
<Style TargetType="ItemsControl">
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="checkbox">
<Setter Property="ItemsControl.ItemTemplate" Value="{StaticResource CheckboxNode}" />
</DataTrigger>
<DataTrigger Binding="{Binding Type}" Value="textbox">
<Setter Property="ItemsControl.ItemTemplate" Value="{StaticResource TextboxNode}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ItemsControl.Style>
</ItemsControl>
If Type property is in SettingNode class, and SettingCollection is a collection of SettingNode objects, then your binding in the Datatriggers is incorrect. DataTriggers will look for Type property in ItemsControl DataContext (class with SettingCollection). Try to use DataTemplateSelector https://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector(v=vs.110).aspx

DataTemplate Based on Value of Bound Property in ItemsSource of TabControl

Ok. So I have a TabControl object in my xaml that has an ItemsSource value of ItemsSource={Binding OpenTabs} where OpenTabs is an ObservableCollection of type ClosableTab (public ObservableCollection<ClosableTab> OpenTabs { get; set; }) which extends TabItem. I found ClosableTab from here and then have adapted it's view for my own needs.
Primarily I have added a property (and sorry for the confusion in names here) isProperty. This is for a real estate program. Then in my xaml I have the following lines:
<DataTemplate x:Key="PropertyTemplate">
<Grid>
<TextBlock Text="{Binding address}"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="TennantTemplate">
<Grid>
<TextBlock Text="{Binding name}"/>
</Grid>
</DataTemplate>
//... That's in <Windows.Resources>
<TabControl ItemsSource="{Binding OpenTabs}" Grid.Column="1" x:Name="Tabs">
<TabControl.Resources>
<DataTemplate x:Key="DefaultTab">
<ContentControl>
<ContentControl.Triggers>
<DataTrigger Binding="{Binding isProperty}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource PropertyTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding isProperty}" Value="False">
<Setter Property="ContentTemplate" Value="{StaticResource TennantTemplate}" />
</DataTrigger>
</ContentControl.Triggers>
</ContentControl>
</DataTemplate>
</TabControl.Resources>
</TabControl>
I've done some research and found that this is what I need to do if I want to have a certain DataTemplate dependant on the property in ClosableTab called isProperty.
It's not giving me what I want. Can someone please explain to me what I'm doing wrong here? And tell me what I should do? And/or possibly give me an alternative method? I can't think of what I need to change to get the functionality that I need. Thanks in advance.
You need to set DataType on DataTemplate to get it applied automatically to underlying data objects in case you are defining DataTemplate under Resources section.
<DataTemplate DataType="local:ClosableTab">
<ContentControl>
<ContentControl.Triggers>
<DataTrigger Binding="{Binding isProperty}" Value="True">
<Setter Property="ContentTemplate"
Value="{StaticResource PropertyTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding isProperty}" Value="False">
<Setter Property="ContentTemplate"
Value="{StaticResource TennantTemplate}" />
</DataTrigger>
</ContentControl.Triggers>
</ContentControl>
</DataTemplate>
Make sure to declare local namespace at root level to the one where ClosableTab is declared.
OR
Instead of adding DataTemplate in resources, set it explicitly as ItemTemplate of TabControl.
<TabControl>
<TabControl.ItemTemplate>
<DataTemplate x:Key="DefaultTab">
.....
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
UPDATE
Ideal case would be to have single DataTemplate and apply dataTrigger on TextBlock instead.
<TabControl ItemsSource="{Binding OpenTabs}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="{Binding address}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding isProperty}"
Value="False">
<Setter Property="Text" Value="{Binding name}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>

How can I know if item is selected inside the ItemTemplate?

I have a ListBox that use my custom ItemTemplate. I want to set Visibility property in my TextBlock (inside my template) depending on selected item. I think of doing it using triggers. But how can I know inside my template if current item is selected or not?
<DataTemplate x:Key="myTemplate">
<StackPanel Orientation="Horizontal">
<Image Tag="{Binding priority}" Loaded="SetIconPriority"/>
<Image Tag="{Binding alarm}" Loaded="SetIconAlarm"/>
<!-- I want this TextBlock to be visible only when item is selected -->
<TextBlock Text="{Binding description}"/>
</StackPanel>
</DataTemplate>
edit:
It works, thanks! Code:
<TextBlock Grid.Column="2" Grid.Row="1" Text="{Binding opis}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Using a RelativeSource binding with AncestorType being ListBoxItem.
<DataTrigger Binding="{Binding IsSelected,
RelativeSource={RelativeSource AncestorType=ListBoxItem}}"
Value="True">
(May want to reverse the logic and Collapse on False instead, avoids the default value Setter)

WPF: Change background on some combobox items

I'm trying to change the background of certain items in a combobox that meet a condition
<ComboBox ItemsSource="{Binding Path=Model.Names, Mode=OneWay}" SelectedValue="{Binding Path=SelectedCompanyName}" DisplayMemberPath="Alias" />
The thing is that "Alias" is saved in two different places (in company and in order) and if they dont match we want to highlight this.
I want to do something like this:
<Style>...
<DataTrigger Binding="{Binding Path=isMismatch}" Value="True>
<Setter Property="Background" Value="Red" />...
Any help is appreciated.
You need to create custom data template like this:
<ComboBox Width="300" Height="30" ItemsSource="{Binding Path=Model.Names, Mode=OneWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="templateGrid">
<TextBox Text="{Binding Name}" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding isMismatch}" Value="True">
<Setter TargetName="templateGrid"
Property="Background" Value="Red" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
If you want to highlight the selection based on the values of two properties, I think you could use a MultiValueConverter, together with a MultiBinding.

Categories