In my application I have the following definition of a DataGridTemplateColumn:
<DataGridTemplateColumn Header="Placeholder-Name" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox IsEditable="True" VerticalAlignment="Center" Margin="2"
ItemsSource="{Binding DataContext.AvailablePlaceholders, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
DisplayMemberPath="PlaceholderName"
Validation.ErrorTemplate="{StaticResource ComboBoxErrorTemplate}">
<ComboBox.Text>
<Binding Path="PlaceholderName" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:PlaceholderValidationRule>
<local:PlaceholderValidationRule.PlaceholderValidationRuleParamData>
<local:PlaceholderValidationRuleParamData
UsedPlaceholders="{Binding Source={StaticResource proxy}, Path=Data.PlaceholderItems}"
AvailablePlaceholders="{Binding Source={StaticResource proxy}, Path=Data.AvailablePlaceholders}"/>
</local:PlaceholderValidationRule.PlaceholderValidationRuleParamData>
</local:PlaceholderValidationRule>
</Binding.ValidationRules>
</Binding>
</ComboBox.Text>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Everything works fine with this, but I just realized that if PlaceholderValidationRule reports a not valid result, the property PlaceholderName in my ViewModel where the ComboBox-Text is bound to will not be updated.
Is there any way to update the property in the ViewModel from the View also if there are Validation-Errors?
Is there any way to update the property in the ViewModel from the View also if there are Validation-Errors?
Set the ValidationStep property of the ValidationRule to UpdatedValue:
<local:PlaceholderValidationRule ValidationStep="UpdatedValue">
This will cause the validation rule to be run after the source property has been updated.
Related
I define a headertemplate into a wpf groupbox and the databinding doesn't work. I don't understand why.
<GroupBox>
<GroupBox.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Image Source="/PopuAssuNetApplication.UI.Control;component/Images/Members.png" Width="24" />
<TextBlock VerticalAlignment="Center">
<TextBlock.Text>
<MultiBinding StringFormat="{x:Static Member=resx:Resources.PersonsInContractGroupBox}">
<Binding Path="CurrentContract.Federation" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}">
</Binding>
<Binding Path="CurrentContract.Type" Converter="{StaticResource contractTypeConverter}" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}">
</Binding>
<Binding Path="CurrentContract.Number" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}">
</Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<WpfComponent:WaitControl Margin="7,0,0,0" VerticalAlignment="Top" Width="24" Height="24" MarginCenter="4">
<WpfComponent:WaitControl.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsMembersOfContractBusy, UpdateSourceTrigger=PropertyChanged, ElementName=PersonsInContract}" Value="true">
<Setter Property="WpfComponent:WaitControl.Visibility" Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsMembersOfContractBusy, UpdateSourceTrigger=PropertyChanged, ElementName=PersonsInContract}" Value="false">
<Setter Property="WpfComponent:WaitControl.Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</WpfComponent:WaitControl.Style>
</WpfComponent:WaitControl>
</StackPanel>
</DataTemplate>
</GroupBox.HeaderTemplate>
The problem is that the HeaderTemplate is used for templating the Header thus within the HeaderTemplate your DataContext is whatever you bind or assign to the Header property of your GroupBox.
Think of the Header property as almost like the DataContext for the header of the control. Normally the DataContext property inherits its value from its parent but since not every control has a Header the Header is blank unless you set it.
By binding your Header explicitly to the current DataContext Header="{Binding}" your example should work as you expect. To help illustrate how this works I've created a simple example below that shows how the Header and DataContext work independently from each other for providing data to either the body or header of the control.
<GroupBox Header="HEADER TEXT" DataContext="BODY TEXT">
<GroupBox.HeaderTemplate>
<DataTemplate>
<Button Content="{Binding}"
Background="LightGreen" />
</DataTemplate>
</GroupBox.HeaderTemplate>
<CheckBox HorizontalAlignment="Center"
VerticalAlignment="Center" Content="{Binding}" />
</GroupBox>
This will yield a GroupBox that looks like the following.
I think that by default in databinding, wpf always gets data from the DataContext property. Seems not in datatemplate
Your assumption is correct about DataContext and it does work in the DataTemplate as I've demonstrated it's just that in the Header's template the DataContext is the value from the Header Property and not the DataContext itself.
The GroupBox does not have a member called "CurrentContract". Most probably, you want to accesss a property called "CurrentContract" from the corresponding ViewModel?! The ViewModel is the GroupBox's DataContext, so you have to change the Binding Paths to something like...
<Binding Path="DataContext.CurrentContract.Type" Converter="{StaticResource contractTypeConverter}" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}">
<GroupBox >
<GroupBox.HeaderTemplate>
<DataTemplate>
<RadioButton Content="myR"
IsChecked="{Binding rIsChecked, Mode=TwoWay}"
DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}}" />
</DataTemplate>
</GroupBox.HeaderTemplate>
<GroupBox.Content>
<Grid IsEnabled="{Binding rIsChecked}">
</Grid>
</GroupBox.Content>
</GroupBox>
Just propagate the GroupBox DC to the DataTemplate content...works like a charm...
The lesson learned above is useful in general for DataTemplates, but I actually found out recently there is a better way to change the header of a groupbox:
<GroupBox>
<GroupBox.Header>
<CheckBox IsChecked="{Binding Path=mSomeBoolean}"/>
</GroupBox.Header>
</GroupBox>
This way there is no need to define a relative source in the bindings.
Also please note this issue with GroupBoxes and the header.
This is what worked for me:
<HeaderedContentControl Header="{Binding}" Style="{StaticResource TallHeaderedContentStyle}">
<HeaderedContentControl.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=HeaderText"} />
</DataTemplate>
</HeaderedContentControl.HeaderTemplate>
I'm trying to get a multi-bound listbox to call the convert back method when items inside it change. Specifically I have a datatemplate that maps each entry to a checkbox and I'd like the source to be updated whenever a box is checked.
<ListBox x:Name="listBoxEdit" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="3" Height="100" >
<ListBox.ItemsSource>
<MultiBinding Converter="{StaticResource selectedLoadsConverter}" >
<Binding Path="AvailableLoads"/>
<Binding Path="CurrentUnit"/>
</MultiBinding>
</ListBox.ItemsSource>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Item1}" IsChecked="{Binding Item2}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I know the problem lies in the fact that I'm binding to to the ItemsSource property which is never actually being updated by the checkbox changing, but I can't figure out a good way to push the updates up to the source.
In the question c# wpf - cannot set both DisplayMemberPath and ItemTemplate, I have read that you can replace the DisplayMemberPath to combine multiple data items in a ComboBox.
I set up my Combobox using ItemTemplate and it is successfully populated. But when I select an item in my ComboBox, I get Data.MaterialNumber displayed instead of the actual text I selected.
MaterialNumbers is an ObservableCollection<>
Can someone tell me why my ComboBox is not displaying the item correctly after it is selected?
// Binding from my ViewModel, which retrieves material numbers correctly.
ObservableCollection<MaterialNumber> MaterialNumbers = GetMaterialNumbers();
<ComboBox Grid.Row="0" Grid.Column="1" ItemsSource="{Binding MaterialNumbers}"
SelectedItem="{Binding MaterialNumber}"
Validation.Error="View_Validator" Validation.ErrorTemplate="{StaticResource ErrorTemplateBorder}"
IsEnabled="{Binding IsEnabled}" IsEditable="True" IsReadOnly="True" Margin="0,10,0,0">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}: {1}">
<Binding Path="Program.Name" />
<Binding Path="MaterialNumberString" />
<Binding UpdateSourceTrigger="PropertyChanged" />
<Binding NotifyOnValidationError="True" />
<Binding ValidatesOnDataErrors="True" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Thanks to this article http://www.shujaat.net/2010/08/wpf-editable-combobox-with-datatemplate.html, I was able to figure out why my ComboBox did not display correctly when an item was selected. It is because my ComboBox was set to IsEditable="True". Apparently, when using a ComboBox.ItemTemplate and having MultiBinding set, the ComboBox cannot determine which item to display so it displays the class instead.
I want to show in one combobox content from two database columns. I would like to show "Name Surname" but I don't know how. I'm working in C# (.NET) using MVVM pattern. "Name" and "Surname" are fields from table "tblGuests".
Thanks in advance,
Vladimir
You could create an ItemTemplate for the ComboBox that binds to all the properties you want to show.
<ComboBox ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstName}" Padding="10,0,0,0"/>
<TextBlock Text="{Binding Path=LastName}" Padding="10,0,0,0"/>
</StackPanel>
<DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
You could even create a UserControl that can be re-used and use that:
<PersonView>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstName}" Padding="10,0,0,0"/>
<TextBlock Text="{Binding Path=LastName}" Padding="10,0,0,0"/>
</StackPanel>
</PersonView>
<ComboBox ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<PersonView/>
<DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Keep a string list property in Viewmodel,
populate that property in a data loading method, by combining name and surname strings,
bind string property to combo box,
<ComboBox Name="ComboBox1" ItemsSource="{Binding YourStringListProperty}"/>
Another option could be to use MultiBinding something like this
<TextBlock Name="textBox2" DataContext="{StaticResource NameListData}">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource myNameConverter}"
ConverterParameter="FormatLastFirst">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
This code was taken directly from msdn. Refer to it for more details
I just started to programm in C# and got the following error message "A 'MultiBinding' cannot be used within a 'SetterBaseCollection' collection. A 'MultiBinding' can only be set on a DependencyProperty of a DependencyObject". My dea was to disable some of the combobox depedant of the input values given to the converter. use the same converter later in the xaml file and have no error..
<DataTemplate x:Key="ComboBoxDirectionCellDataTemplate">
<Canvas>
<ComboBox Name="DirectionBi" ItemsSource="{Binding Source={StaticResource DirectionBiList}}" SelectedItem="{Binding Direction, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Background="#FFCDCDCD" />
<ComboBox Name="DirectionOut" ItemsSource="{Binding Source={StaticResource DirectionOutList}}" SelectedItem="{Binding PinFunctionOptions[SelectedPinFunctionIdx].SupportedDirections, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Background="#FFCDCDCD" />
<ComboBox Name="DirectionIn" ItemsSource="{Binding Source={StaticResource DirectionOutList}}" SelectedItem="{Binding PinFunctionOptions[SelectedPinFunctionIdx].SupportedDirections, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Background="#FFCDCDCD" />
</Canvas>
<DataTemplate.Triggers>
<DataTrigger Value="In">
<MultiBinding Converter="{StaticResource FunctionToGroupConverter}">
<Binding Path="PinFunctionOptions"/>
<Binding Path="SelectedPinFunctionIdx"/>
</MultiBinding>
<Setter TargetName="DirectionBi" Property="Visibility" Value="Hidden"/>
Thanks for help!!!!
I may be wrong - and i have no way of checking this at the moment...
I think what you want is the following...
<DataTrigger ...>
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource FunctionToGroupConverter}">
<Binding Path="PinFunctionOptions"/>
<Binding Path="SelectedPinFunctionIdx"/>
</MultiBinding>
</DataTrigger.Binding>
</DataTrigger>
DataTemplate.Triggers is expecting a collection of Setter why are you attempting to put a MultiBinding in there?
The exception you're getting is a result of this.
I'm not entirely certain what you're attempting with the MultiBinding but you may fare better by performing the bindings individually on the respective elements.