wpf devexpress gridcontrol with radiobutton, pass IsEnabled binding to celltemplate - wpf

I have a Gridcolumn like this:
<dxg:GridColumn Binding="{Binding OrderTyp, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Header="Order"
Visible="{Binding IsOrderToMe}"
Width="60"
IsEnabled="{Binding StatusHandled, Converter={StaticResource InvertedBooleanConverter}}"
CellTemplate="{StaticResource IsOrderTyp}"/>
My celltemplate looks like this:
<DataTemplate x:Key="IsOrderTyp">
<RadioButton IsChecked="{styles:GridCellValueBinding
Converter={StaticResource ECompare},
ConverterParameter={x:Static enums:OrderTypIdEnum.ORDER}, Mode=TwoWay}"
IsEnabled="{Binding ????????????}"
Style="{StaticResource GridRadioButtonStyle}"/>
</DataTemplate>
how do I get the binding for IsEnabled from the gridcolumn in my celltemplate?

Related

Binding between two DataTemplate works in one direction only

I have a DataGrid where there are Date0, Date1, and Date2 columns. I can change the columns with a ComboBox.
I started to use DataTemplate. The CellTemplate uses Date0InParameterEditingTemplate where I bind Date0 which is not simple DateTime but a class. Then this class is bound to DateEditingTemplate. I would like to use DateEditingTemplate later with Date1 and Date2 as well.
The data goes well to one direction, so the ComboBox displays the proper date. When I select a new value in the ComboBox then the SelectedItem changes. But the content of ContentControl remains unchanged.
<DataGridTemplateColumn Header="{x:Static r:Resource.Date0}"
CellTemplate="{StaticResource Date0InParameterEditingTemplate}"/>
<DataTemplate x:Key="DateEditingTemplate">
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DataGrid}}, Path=DataContext.ValidDateObjects}"
SelectedItem="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Date" Style="{StaticResource ComboBoxError}" IsEditable="False"
ItemStringFormat="{StaticResource DateFormatHU}" />
</DataTemplate>
<!--<DataTemplate x:Key="DateEditingTemplate">
<TextBlock Text="{Binding Date}"/>
</DataTemplate>-->
<DataTemplate x:Key="Date0InParameterEditingTemplate">
<ContentControl Content="{Binding Date0, ValidatesOnDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ContentTemplate="{StaticResource DateEditingTemplate}"/>
</DataTemplate>

Automatically change column width in xaml

I have a stackpanel with checkbox and combobox:
<StackPanel Name="PointSizeFilter"
Style="{StaticResource StackPanelCell}">
<CheckBox ... />
<ComboBox ... />
</StackPanel>
And there is a datagrid. When the stackpanel's width is changed, I want datagrid column to be resized as well.
<DataGrid Style="{StaticResource DataGridEditable}"
ItemsSource="{Binding ElementName=IModel, Path=ControlPoints.Collection}"
Name="DataGridCheckpoints">
<DataGrid.Columns>
...
<DataGridComboBoxColumn Header="{x:Static constants:Labels.ControlPointsObjectSize}"
Width="{Binding ElementName=PointSizeFilter, Path=ActualWidth}"
ItemsSource="{Binding Source={x:Static controlpoints:CpViewModel.PointSizeItems}}"
DisplayMemberPath="Name"
SelectedValuePath="Id"
SelectedValueBinding="{Binding PointSizeId, UpdateSourceTrigger=PropertyChanged}"/>
</DataGrid.Columns>
</DataGrid>
But it doesn't work. What am I doing wrong? Thanks!
One possible reason is that the Width property on DataGridColumn is not a double as ActualWidth, but a DataGridLength type. Try putting a converter in between.

WPF Datagrid TemplateColumn Control Enable and Disable

I have Datagrid with two TemplateColumn.
First column is a Combobox and Second column with Extended IntergerUpDown control
I need to Enable/Disable the IntegerUpDown control based on the Combox box SelectedItem value.
Please help me how to accomplish this. Sample xaml below.
<Grid><DataGrid ItemsSource="{Binding List1}" Name="x1">
<DataGrid.Columns>
<DataGridTemplateColumn Header="ColorTemplate">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataContext.List2, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"
DisplayMemberPath="Name" SelectedValue="{Binding ColourId}" SelectedValuePath="Id" Tag="{Binding Id}"
HorizontalAlignment="Stretch" x:Name="discussTemplate" VerticalAlignment="Stretch"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="UPDown" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<extToolkit:IntegerUpDown AllowSpin="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Minimum="0"
x:Name="updown"
IsEnabled="????" >
</extToolkit:IntegerUpDown>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
This can be easily done with a DataTrigger on your ViewModel ColourId property.
As your SelectedValue property on the ComboBox is already binded, you can have a DataTrigger on your extToolkit:IntegerUpDown control that will set IsEnabled to True/False based on ColourId value on your ViewModel.

Cascading Combobox in DataGrid using DataTemplate Cross-Cell Binding

If I place my cascading comboboxes inside the same DataTemplate in a WPF DataGrid cell - the binding works properly (via ElementName). However, from a UI perspective I want my comboboxes to physically reside in different cells, not the same datagrid cell. How do you make cross-cell binding work (between DataTemplates) using DataGridTemplateColumns? It seems the issue is that the second comboboxes' ItemsSource cannot find the ElementName for binding when the comboboxes exist in different DataTemplate columns.
This works....
<DataGrid x:Name="grdItems" AutoGenerateColumns="false" ItemsSource="{Binding Model}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Car Make / Model" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cbCarMake" SelectedItem="{Binding CarMake, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.CarMakes, Mode=TwoWay}" DisplayMemberPath="Name" SelectedValuePath="ID">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<mvvm:EventToCommand Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.SelectCarMake}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
<ComboBox SelectedItem="{Binding CarModel, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding ElementName=cbCarMake, Path=Tag.CarModels, Mode=TwoWay}" DisplayMemberPath="Name" SelectedValuePath="ID"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
This doesn't....
<DataGrid x:Name="grdItems" AutoGenerateColumns="false" ItemsSource="{Binding Model}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Car Make" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cbCarMake" SelectedItem="{Binding CarMake, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.CarMakes, Mode=TwoWay}" DisplayMemberPath="Name" SelectedValuePath="ID">
<i:Interaction.Triggers>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<mvvm:EventToCommand Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.SelectCarMake}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</i:Interaction.Triggers>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Car Model" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding CarModel, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding ElementName=cbCarMake, Path=Tag.CarModels, Mode=TwoWay}" DisplayMemberPath="Name" SelectedValuePath="ID"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
The trick with cross-data template binding is to use the ViewModel as a go-between. When properties change on the view model (an ObservableCollection in this case), you can trigger this notification to the dependent listeners (elements listening to PropertyChanged events).
The issue for me was that the View Model wasn't receiving the PropertyChanged notifications it should have been. I assumed that the mvvm-light ObservableObject automatically triggered PropertyChanged events for all properties.
You must use the mvvminpcset code snippet which explicitly raises property change events for the items in your ObservableCollection. Once the PropertyChanged events started firing, the ItemSource binding worked as seen below.
<DataGrid x:Name="grdItems" AutoGenerateColumns="false" ItemsSource="{Binding Model}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Car Make" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cbCarMake" SelectedItem="{Binding CarMake, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.CarMakes, Mode=TwoWay}" DisplayMemberPath="Name" SelectedValuePath="ID">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Car Model" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding CarModel, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding CarMake.CarModels}" DisplayMemberPath="Name" SelectedValuePath="ID"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

C# WPF two different DataContext in One Control while using MVVM and the secound property is every time the same

First the code:
<ListView ItemsSource="{Binding DataTransferModel.Output}" Background="Transparent" Margin="0" VerticalContentAlignment="Top" AlternationCount="2" Name="lvOutput" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="2">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,1">
<UserControls:OutputTextBox Text="{Binding Data, Mode=OneWay}"
IsReadOnly="True"
Grid.Row="2"
TextWrapping="WrapWithOverflow"
SelectedValue="{Binding Path=DataContext.SelectedOutput,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}
}"
/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And the problem is that the property Data on the OutputTextBox control comes from the list, but the property SelectedOutput should come from the main DataContext the ViewModel. And the property SelectedOutput should be the same for every entry in the list. But currently it dosn't work. :(
does your listview is enclosed in tags and if yes does the grid has the DataContext from the ViewModel? if yes it should work.
do you see any binding errors in your output window? maybe you should try using Snoop to see your real binding of SelectedValue.
EDIT: Please try a Type other then Grid, maybe ListView if it has the ViewModel DataContext.
<UserControls:OutputTextBox Text="{Binding Data, Mode=OneWay}"
IsReadOnly="True"
Grid.Row="2"
TextWrapping="WrapWithOverflow"
SelectedValue="{Binding Path=DataContext.SelectedOutput,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}
}"
/>

Resources