Automatically change column width in xaml - wpf

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.

Related

WPF - Binding a ComboBoxItem visibility using BooleanToVisibilityConverter

I have a Datagrid, with a column that has a combobox in its header. I want to control the visibility of items in the combobox based on the value of a certain flag.
This is how my XAML looks:
<DataGrid x:Name="Table1" Height="{Binding ElementName=ElasticOne1,Path=ActualHeight}" Width="{Binding ElementName=ElasticOne1,Path=ActualWidth}" Padding="5,5,5,5" IsReadOnly="True"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectionMode="Single" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="Column2" Header="Source" Width="80">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Margin="0">
<StackPanel.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis"/>
</StackPanel.Resources>
<TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}" Margin="5"/>
<ComboBox HorizontalAlignment="Stretch" Margin="0" >
<ComboBoxItem Tag="0" IsSelected="True">All</ComboBoxItem>
<ComboBoxItem Visibility="{Binding ShowCopybookInSourceCombobox, Converter={StaticResource BoolToVis}}">Copybook</ComboBoxItem>
<ComboBoxItem>Prototype</ComboBoxItem>
</ComboBox>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
And this is the declaration of the flag in code:
Public ReadOnly Property ShowCopybookInSourceCombobox As Boolean
Get
Return myFlag1 Or myFlag2
End Get
End Property
No matter what the value of my flag, the combobox item is always showing. I haven't seen any examples so far of binding the visibility of a comboboxitem this way. Am I on the wrong path?
Thanks for all your input.
Edit: BooleanToVisibilityConverter is a built-in class that I am using - https://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter(v=vs.110).aspx

WPF XAML: Why SelectedItem is not working on ListBox when I click ComboBox in it?

Why SelectedItem is not working on ListBox when I click ComboBox in it, below the code:
<ListBox SelectedItem="{Binding MySelectedItemDataGrid}>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<ComboBox />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I need to get the SelectedItem in ListBox, when I click ComboBox.
Thank you
Instead of using ListBox I am using DataGrid.
<DataGrid ItemsSource="{Binding MyItemsSourceDataGrid}"
HeadersVisibility="None"
GridLinesVisibility="None"
SelectedItem="{Binding MySelectedItemDataGrid}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding MyItemsSourceComboBox}"
SelectedItem="{Binding MySelectedItemComboBox, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Thank you.

DataGrid Edit and Readonly behaviour in wpf

I m new to WPf Datagrid .My requirement if i select a cell in datagrid and press enter it should changed to readonly= false(editable) and user can change the content and again if he press enter key the cell should change to readonly = true(noneditable).
My xaml look like this:
<DataGrid AutoGenerateColumns="False" Height="496" HorizontalAlignment="Left"
PreviewKeyDown="DgvMaterial_PreviewKeyDown" DataGridCell.Selected="DataGrid_select"
Name="DgvMaterial" VerticalAlignment="Top" Width="958" Margin="21,20,0,0"
ItemsSource="{Binding Path=., Mode=OneWay}"
AlternationCount="1" AlternatingRowBackground="#FFE9FFE9"
SelectionUnit="FullRow" CanUserResizeColumns="False" DataContext="{Binding}"
RowHeight="30" UseLayoutRounding="True"
RowHeaderWidth="0" OverridesDefaultStyle="False" ColumnHeaderHeight="30"
SelectionMode="Single" ScrollViewer.VerticalScrollBarVisibility="Auto"
FontFamily="MS Gothic" FontSize="12" CellStyle="{StaticResource Body_Content_DataGrid_Centering}"
IsHitTestVisible="True" SelectionChanged="DgvMaterial_SelectionChanged"
Loaded="DgvMaterial_Loaded" CellEditEnding="DgvMaterial_CellEditEnding" TabIndex="5"
SelectedIndex="-1" IsReadOnly="True">
Probably what you're looking for is DataGridTemplateColumn which can have 2 templates. One is to show value (DataGridTemplateColumn.CellTemplate) and the other is used in edit mode (DataGridTemplateColumn.CellEditingTemplate):
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=.}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Column">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=ColumnName, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ColumnName}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
You can specify it per column, not for grid, and by default it will go into edit mode on F2 or double click but you can handle DataGrid.PreviewKeyDown and BeginEdit() on Enter

WPF DataGrid Element Name of other DataGridTemplateColumn

I have two Datagrid Template column.
1) Fist column is a Combobox
2) Second column with Extended IntergerUpDown control
I need to set the IntegerUpDown Maximum value 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"
Maximum="{Binding ???}" >
</extToolkit:IntegerUpDown>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
simply bind to the value of your itemsrow? so if ColourID is the property of your itemsrow which is set by your combobox. you can bind your Maximum to ColourID
<extToolkit:IntegerUpDown AllowSpin="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Minimum="0"
Maximum="{Binding ColourId}" >
</extToolkit:IntegerUpDown>

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.

Resources