I have a ControlTemplate declared in UserControl.Resources. So I know how to add that control to my control(DataGrid in this case) like this:
MyDataGrid.Template = (ControlTemplate)this.FindResource("inputItemsControlTemplate");
Now I want to remove this ControlTemplate in code behind. Actually what I'm trying to manage is to change the look of DataGrid to it's previous state, as it was before this line of code.
Update:
Ok I tried to be as simpler as possible but I'll describe the full stuff here. I have a datagrid defined with all of it's binding and event handlers and all the other stuff. Also I have a ControlTemplate defined in my . This ControlTemplate is actualy a form which I use for inserting new data to datagrid. Here is the code of my template:
<ControlTemplate x:Key="inputItemsControlTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<Label Grid.Row="1" Content="{DynamicResource UCodeStr}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="27" />
<TextBox Name="txtUCode" Grid.Row="2" Height="23" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="100" Text="{Binding UCode, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<Label Grid.Row="1" Content="{DynamicResource GoodStr}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="27"/>
<ComboBox Grid.Row="2" Height="23" Width="189" HorizontalAlignment="Left" Name="cbGoods" ItemsSource="{Binding Path=Goods}" SelectedItem="{Binding Path= Good, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name" IsEditable="True" IsTextSearchEnabled="True" TextSearch.TextPath="Name" />
<Label Grid.Row="3" Content="{DynamicResource AmmountStr}" HorizontalAlignment="Left" Name="lblAmmount" VerticalAlignment="Bottom" Height="27"/>
<TextBox Name="txtAmmount" TextAlignment="Right" Grid.Row="4" Height="23" Width="189" HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Path=Amount, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, StringFormat='N2'}" />
</Grid>
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<Label Grid.Row="1" Content="{DynamicResource InputPriceStr}" HorizontalAlignment="Left" Name="lblInputPrice" VerticalAlignment="Bottom" Height="27"/>
<TextBox Name="txtInputPrice" Grid.Row="2" TextAlignment="Right" Height="23" Width="189" HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Path= InputPrice, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True, StringFormat='N2'}" />
<Label Grid.Row="3" Content="{DynamicResource SuggestedPriceStr}" HorizontalAlignment="Left" Name="lblSuggestedPrice" VerticalAlignment="Bottom" Height="27"/>
<TextBox Name="txtSuggestedPrice" Grid.Row="4" TextAlignment="Right" Height="23" Width="189" HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Path= SuggestedPrice, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, StringFormat='N2'}" />
<CheckBox Grid.Row="5" Name="cbHasVatDeduction" IsChecked="{Binding Path=HasVatDeduction}" />
</Grid>
</Grid>
</ControlTemplate>
I have some add button. In it's Click Event Handler I do this:
MyDataGrid.Template = (ControlTemplate)this.FindResource("inputItemsControlTemplate");
The effect I get with this is my DataGrid is replaced with those textboxes and combobox for inserting new data. What I want to do is to switch back to my default datagrid look without using another template. I'm doing that right now but it is not good for me becouse the events in my datagrid. Here is the code of my datagrid:
<DataGrid AutoGenerateColumns="False"
IsReadOnly="True"
Name="InputDocItemsDataGrid"
ItemsSource="{Binding Path= InputItems}"
SelectedItem="{Binding Path= InputItem, UpdateSourceTrigger=PropertyChanged}"
SelectionChanged="InputDocItemsDataGrid_SelectionChanged"
PreviewMouseLeftButtonDown="InputDocItemsDataGrid_PreviewMouseLeftButtonDown">
<DataGrid.Columns>
<DataGridTemplateColumn CanUserReorder="False" CanUserResize="False">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox Name="cbxAll" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="cbxAll_Checked" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="cbxSingleRow" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" PreviewMouseLeftButtonDown="cbxSingleRow_PreviewMouseLeftButtonDown" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="{DynamicResource StockStr}" Binding="{Binding Path= tblInputDoc.tblStock.Name}" />
<DataGridTextColumn Header="{DynamicResource UCodeStr}" Binding="{Binding Path= tblGood.UCode}" />
<DataGridTextColumn Header="{DynamicResource GoodStr}" Binding="{Binding Path= tblGood.Name}" />
<DataGridTextColumn Header="{DynamicResource AmmountStr}" Binding="{Binding Path= Amount, Converter={StaticResource moneyConverter}}" />
<DataGridTextColumn Header="{DynamicResource InputPriceStr}" Binding="{Binding Path= InputPrice, Converter={StaticResource moneyConverter}}" />
<DataGridTextColumn Header="{DynamicResource SuggestedPriceStr}" Binding="{Binding Path= SuggestedPrice, Converter={StaticResource moneyConverter}}" />
<DataGridTextColumn Header="{DynamicResource InputValueStr}" Binding="{Binding Path= InputValue, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
<DataGridTextColumn Header="{DynamicResource VatBaseStr}" Binding="{Binding Path= VatBase, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
<DataGridTextColumn Header="{DynamicResource VatValueStr}" Binding="{Binding Path= VatValue, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
<DataGridTextColumn Header="{DynamicResource InputWithVatStr}" Binding="{Binding Path= InputPriceWithVat, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
<!--<DataGridTextColumn Header="Stock" Binding="{Binding Path= tblStock.Name}" />-->
</DataGrid.Columns>
</DataGrid>
And the code of my second ControlTemplate which I use to switch to previous look of datagrid:
<ControlTemplate x:Key="baseDataGridTemplate">
<DataGrid AutoGenerateColumns="False"
IsReadOnly="True"
Name="InputDocItemsDataGrid"
ItemsSource="{Binding Path= InputItems}"
SelectedItem="{Binding Path= InputItem, UpdateSourceTrigger=PropertyChanged}"
SelectionChanged="InputDocItemsDataGrid_SelectionChanged"
PreviewMouseLeftButtonDown="InputDocItemsDataGrid_PreviewMouseLeftButtonDown">
<DataGrid.Columns>
<DataGridTemplateColumn CanUserReorder="False" CanUserResize="False">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox Name="cbxAll" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="cbxSingleRow" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="{DynamicResource StockStr}" Binding="{Binding Path= tblInputDoc.tblStock.Name}" />
<DataGridTextColumn Header="{DynamicResource UCodeStr}" Binding="{Binding Path= tblGood.UCode}" />
<DataGridTextColumn Header="{DynamicResource GoodStr}" Binding="{Binding Path= tblGood.Name}" />
<DataGridTextColumn Header="{DynamicResource AmmountStr}" Binding="{Binding Path= Amount, Converter={StaticResource moneyConverter}}" />
<DataGridTextColumn Header="{DynamicResource InputPriceStr}" Binding="{Binding Path= InputPrice, Converter={StaticResource moneyConverter}}" />
<DataGridTextColumn Header="{DynamicResource SuggestedPriceStr}" Binding="{Binding Path= SuggestedPrice, Converter={StaticResource moneyConverter}}" />
<DataGridTextColumn Header="{DynamicResource InputValueStr}" Binding="{Binding Path= InputValue, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
<DataGridTextColumn Header="{DynamicResource VatBaseStr}" Binding="{Binding Path= VatBase, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
<DataGridTextColumn Header="{DynamicResource VatValueStr}" Binding="{Binding Path= VatValue, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
<DataGridTextColumn Header="{DynamicResource InputWithVatStr}" Binding="{Binding Path= InputPriceWithVat, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
<!--<DataGridTextColumn Header="Stock" Binding="{Binding Path= tblStock.Name}" />-->
</DataGrid.Columns>
</DataGrid>
</ControlTemplate>
I really hope you get the point of what I'm trying to do. I can't be more specific than this and I wanted to avoid the amount of code in the question. If this is not enoughf than the question should be deleted.
I have no idea what you talking about it seems like you got super lost in wpf's world :)
Take a look at this:
this.previousTemplate = MyDataGrid.Template;
MyDataGrid.Template = (ControlTemplate)this.FindResource("baseDataGridTemplate");
and then at some point later...
MyDataGrid.Template = previousTemplate;
If you want to do this through code behind then before you assign your custom ControlTemplate save your MyDataGrid let's say
var defaultDataGrid = MyDataGrid;
//then
MyDataGrid.Template = (ControlTemplate)this.FindResource("baseDataGridTemplate");
//and when you want to change it back
MyDataGrid.Template = defaultDataGrid.Template
let us know how it went :-)
But to be perfectly honest you could do this in xaml using DataTriggers in DataGrid Style
Related
I have a popupbox which is shown on every row and when I click on that popup it should show the more details of the selected row. I was able to get values of selected row but I am unable to bind them to popupbox controls.
<DataGrid x:Name="CRDataGrid" AutoGenerateColumns="False" SelectionMode="Single" SelectedItem="{Binding SelectedCR}" FontSize="14" CanUserAddRows="False" ItemsSource="{Binding crentities}"
CanUserDeleteRows="False" md:DataGridAssist.ColumnHeaderPadding="4" md:DataGridAssist.CellPadding="4" SelectionUnit="FullRow" MouseDoubleClick="DataGrid_MouseDoubleClick" RowDetailsVisibilityMode="VisibleWhenSelected" HeadersVisibility="All" Grid.ColumnSpan="2" Grid.Row="1" Margin="20" >
<DataGrid.Columns>
<DataGridTextColumn Header="Title" IsReadOnly="True" Binding="{Binding LogName}" Width="80" />
<DataGridTextColumn Header="MUIdentifier" IsReadOnly="True" Binding="{Binding MU_Identifier}" Width="100" />
<DataGridTextColumn Header="Status" IsReadOnly="True" Binding="{Binding Status}" Width="80" />
<DataGridTextColumn Header="RequestType" IsReadOnly="True" Binding="{Binding RequestType}" Width="100" />
<DataGridTextColumn Header="DateTime" IsReadOnly="True" Binding="{Binding Create_Date,TargetNullValue='-'}" Width="100" />
<DataGridTextColumn Header="SoftwareVersion" IsReadOnly="True" Binding="{Binding SW_Version}" Width="200" />
<DataGridTemplateColumn >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<md:PopupBox DockPanel.Dock="Right" PlacementMode="BottomAndAlignRightEdges" StaysOpen="True">
<md:PopupBox.ToggleContent>
<md:PackIcon Kind="DotsHorizontal" Margin="4 0 4 0" Width="24" Height="24"
Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=md:PopupBox}, Path=Foreground}" />
</md:PopupBox.ToggleContent>
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding ElementName=CRDataGrid, Path= SelectedCR.MU_Identifier}" />
</md:PopupBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Shouldn't
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding ElementName=CRDataGrid, Path= SelectedCR.MU_Identifier}" />
instead be
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding ElementName=CRDataGrid, Path= SelectedItem.MU_Identifier}" />
?
I have a problem with a StackPanel and its visibility in a DatagridCellTemplateColumn.
My ControlTemplate looks like this:
<ControlTemplate x:Key="PartPrioritySettingsGridCellTemplate">
<StackPanel Name="CellGrid" Orientation="Horizontal" Tag="{TemplateBinding Tag}"
Visibility="{Binding RelativeSource={RelativeSource Self},
Path=Tag.IsNotEmpty, Converter={StaticResource BoolToVisibility}}">
<CheckBox Margin="4,1,2,1"
IsChecked="{Binding Path=Tag.Items.IsChecked, ElementName=CellGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<xceed:IntegerUpDown Margin="2,1" Maximum="99" Minimum="1"
Value="{Binding Path=Tag.Priority, ElementName=CellGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Button Margin="2,1,4,1" Command="{Binding Path=Tag.ShowDetailCommand, ElementName=CellGrid}"
Content="{Binding Path=Tag.ItemsCountString, ElementName=CellGrid}"
ContentStringFormat="{}{0:}" />
</StackPanel>
</ControlTemplate>
Usage of this template looks like this:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Width="auto" Tag="{Binding Webs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
I want to make a DataGrid where the width of all columns is set to the width of the current content. But when the content of a column is collapsed, then the width of a column should be given only by the width of the header. Actually I don't know what is the problem in my solution, but all my columns have the same width, even if their content is collapsed.
What I'm trying to achieve looks like this:
What I'm getting looks like this:
And this is code of the DataGrid
<DataGrid Grid.Row="0"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
ItemsSource="{Binding JobBomPriorityRows,
UpdateSourceTrigger=PropertyChanged}">
<!-- -->
<DataGrid.Resources>
<vc:BoolToVisibilityConverter x:Key="BoolToVisibility" />
<ControlTemplate x:Key="PartPrioritySettingsGridCellTemplate" TargetType="Control">
<StackPanel Name="CellGrid"
Width="auto"
Orientation="Horizontal"
Tag="{TemplateBinding Tag}"
Visibility="{Binding RelativeSource={RelativeSource Self},
Path=Tag.IsNotEmpty,
Converter={StaticResource BoolToVisibility}}">
<CheckBox Margin="4,1,2,1" IsChecked="{Binding Path=Tag.Items.IsChecked, ElementName=CellGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<xceed:IntegerUpDown Margin="2,1"
Maximum="99"
Minimum="1"
Value="{Binding Path=Tag.Priority,
ElementName=CellGrid,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" />
<Button Margin="2,1,4,1"
Command="{Binding Path=Tag.ShowDetailCommand,
ElementName=CellGrid}"
Content="{Binding Path=Tag.ItemsCountString,
ElementName=CellGrid}"
ContentStringFormat="{}{0:}"
Padding="3,0" />
</StackPanel>
</ControlTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Width="*"
Binding="{Binding Week}"
Header="Week"
IsReadOnly="True"
SortDirection="Ascending" />
<DataGridTextColumn Width="*"
Binding="{Binding JobBom}"
Header="Job Bom"
IsReadOnly="True" />
<DataGridTemplateColumn Width="auto"
Header="Web"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding Webs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto"
Header="Flange"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding Flanges}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto"
Header="Plate"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding Plates}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto"
Header="Gusset"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding Gussets}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto"
Header="SP"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding SPs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto"
Header="STD G"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding STD_Gs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto"
Header="STD SP"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding STD_SPs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
I have a datagrid. When a row is click on the rowdetails is shown. In the rowdetails I have another datagrid next to the datagrid is a DatePicker.
So the main datagrid has a collection of a custom object. When a row is clicked the sub datagrid is bound to the selecteditem.Funds (another list) (code shown below). The selectedItem also has a datetime property which I want to bind to my DatePicker however I cannot get it to work.
I am using the line below,
<DatePicker SelectedDate="{Binding SelectedItem.DateEffective2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
I assumed (maybe wrongly that because the datepicker is in the rowdetails I would be able to bind to the selectedItem, guess I'm wrong?
<DataGrid Grid.Row="1"
ItemsSource="{Binding HldLogEQCurr, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedHldLogEq, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource DataGridTemplate1}"
ColumnHeaderStyle="{StaticResource DG_ColumnHeaderCenter1}"
RowStyle="{StaticResource DG_Row1}"
CellStyle="{StaticResource DG_Cell1}"
RowHeaderStyle="{StaticResource DG_RowHeader1}"
AutoGenerateColumns="False"
HorizontalAlignment="Stretch"
Background="Silver"
Margin="50,50,50,50"
CanUserDeleteRows="False"
CanUserAddRows="False"
RowHeaderWidth="30">
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Grid x:Name="RowDetailGrid"
Margin="5"
HorizontalAlignment="Left">
<Border HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="250"
CornerRadius="5">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="Transparent"/>
<GradientStop Offset="1" Color="Transparent"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="400"/>
<ColumnDefinition Width="300"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Margin="5,5,5,5"
HorizontalAlignment="Left"
FontSize="12"
FontWeight="Bold"
Foreground="Black"
Text="Select action to take">
</TextBlock>
<DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
ItemsSource="{Binding SelectedItem.Funds, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
RowStyle="{StaticResource DG_Row}"
ColumnHeaderStyle="{StaticResource DG_ColumnHeader}"
RowHeaderStyle="{StaticResource DG_RowHeaderNested}"
CellStyle="{StaticResource DG_Cell}"
Background="Silver"
HorizontalGridLinesBrush="LightGray"
VerticalGridLinesBrush="LightGray"
CanUserAddRows="False"
CanUserDeleteRows="False"
Margin="50,5,5,20"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Fund Code" Binding="{Binding Code}" IsReadOnly="True" MinWidth="75"/>
<DataGridTextColumn Header="Fund Code SS" Binding="{Binding CodeSS}" IsReadOnly="True" MinWidth="75"/>
<DataGridTextColumn Header="Number of Rights" Binding="{Binding CurrentNominal, StringFormat={}{0:N0}}" IsReadOnly="True"/>
<DataGridTextColumn Header="Rights To Exercise" Binding="{Binding NewNominal, StringFormat={}{0:N0}}" IsReadOnly="True"/>
<DataGridCheckBoxColumn Header="Take Cash" Binding="{Binding OptionOne, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False"/>
<DataGridCheckBoxColumn Header="Take Stock" Binding="{Binding OptionTwo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False"/>
<DataGridCheckBoxColumn Header="Take Both" Binding="{Binding OptionThree, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False"/>
</DataGrid.Columns>
</DataGrid>
<Grid Grid.Column="2" Grid.Row="0" Grid.RowSpan="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="180"/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1" Grid.Column="2" Margin="20,0,0,0">
<TextBlock Margin="0,5,5,5" HorizontalAlignment="Left" FontSize="12" FontWeight="Bold" Foreground="Black"
Text="Select Date Effective From"/>
<DatePicker HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,10,0,0"
BorderThickness="0" SelectedDate="{Binding SelectedItem.DateEffective, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="200"/>
</StackPanel>
</Grid>
</Grid>
</Border>
</Grid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.Columns>
<DataGridTextColumn Header="Date Entered" IsReadOnly="True" Binding="{Binding DateEntered, StringFormat={}\{0:dd-MMM-yy\}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="75"/>
<DataGridTextColumn Header="Date Effective" IsReadOnly="True" Binding="{Binding DateEffective, StringFormat={}\{0:dd-MMM-yy\}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="75"/>
<DataGridTextColumn Header="Sedol" IsReadOnly="True" Binding="{Binding Security.Sedol}" MinWidth="75"/>
<DataGridTextColumn Header="Name" IsReadOnly="True" Binding="{Binding Security.Name}" MinWidth="200"/>
<DataGridTextColumn Header="Ratio" IsReadOnly="True" Binding="{Binding RatioNew}" MinWidth="75"/>
</DataGrid.Columns>
</DataGrid>
The DataContext for the DatePicker should already be the SelectedItem, try changing it to:
<DatePicker SelectedDate="{Binding DateEffective2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
Also, your sub-DataGrid's ItemsSource could be simplified from:
ItemsSource="{Binding SelectedItem.Funds, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
To:
ItemsSource="{Binding Funds}"
I have a WPF datagrid that looks like the following:
<DataGrid x:Name="dataGridOrderItems" Margin="369,0,4,51" VerticalAlignment="Bottom" Height="405" Grid.Row="1"
VerticalGridLinesBrush="LightGray" HorizontalGridLinesBrush="LightGray" AlternatingRowBackground="Beige" AlternationCount="2" SelectionMode="Single"
SelectionChanged="dataGridOutstandingOrders_SelectionChanged" AutoGenerateColumns="False" IsReadOnly="True" Grid.ColumnSpan="2">
<DataGrid.Columns>
<DataGridTextColumn Header="Resource Name" Binding="{Binding ResourceName}" />
<DataGridTextColumn Header="Quantity Ordered" Binding="{Binding Quantity}" />
<DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate, StringFormat=\{0:d\}}" />
<DataGridCheckBoxColumn Header="Dispatched Resource" Binding="{Binding IsChecked}" />
<DataGridTextColumn/>
</DataGrid.Columns>
</DataGrid>
Even though the datagrid is enabled and I've specified a selection mode I can't click into any of the cells. What else am I missing?
In case it's relevant, here's the full XAML for the window:
<Window x:Class="OrderProcessor.MainWindow"
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"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:OrderProcessor"
mc:Ignorable="d"
Title="Order Processing" Height="727.625" Width="1088" Icon="1438333970_Delivery.ico">
<Grid Margin="0,0,2,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="9*"/>
<ColumnDefinition Width="14*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="94*"/>
<RowDefinition Height="315*"/>
</Grid.RowDefinitions>
<Button x:Name="buttonRefreshOrders" Content="Load/Refresh Orders List" Margin="0,36,10,0" RenderTransformOrigin="-0.72,-2.65" ToolTip="Click to refresh the orders list with the latest orders from SharePoint" Grid.Column="1" HorizontalAlignment="Right" Width="140" Height="36" VerticalAlignment="Top" Click="buttonRefreshOrders_Click"/>
<DataGrid x:Name="dataGridOutstandingOrders" Margin="11,7,156,470" RenderTransformOrigin="0.5,0.5" Grid.RowSpan="2" Grid.ColumnSpan="2" VerticalGridLinesBrush="LightGray" HorizontalGridLinesBrush="LightGray" AlternatingRowBackground="Beige" AlternationCount="2" SelectionMode="Single" SelectionUnit="FullRow" SelectionChanged="dataGridOutstandingOrders_SelectionChanged" AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Persona" Binding="{Binding Persona }" />
<DataGridTextColumn Header="Customer Name" Binding="{Binding CustomerName }" />
<DataGridTextColumn Header="Organization" Binding="{Binding Organization }" />
<DataGridTextColumn Header="E-mail" Binding="{Binding Email }" />
<DataGridTextColumn Header="Phone" Binding="{Binding PhoneNumber }" />
<DataGridTextColumn Header="Street" Binding="{Binding Street }" />
<DataGridTextColumn Header="Suburb" Binding="{Binding Suburb }" />
<DataGridTextColumn Header="Postcode" Binding="{Binding Postcode }" />
<DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate , StringFormat={}{0:d}}" />
</DataGrid.Columns>
</DataGrid>
<Button x:Name="button_Dispatch_Current_Item" Content="Set selected resources to be dispatched" Margin="0,0,10,10" RenderTransformOrigin="-0.72,-2.65" ToolTip="" HorizontalAlignment="Right" Width="462" Grid.Row="1" Grid.Column="1" Height="36" VerticalAlignment="Bottom"/>
<GroupBox x:Name="groupBoxCustomerDetails" Header="Customer Details" Margin="11,0,0,10" Grid.Row="1"
Grid.Column="0" VerticalAlignment="Bottom" Height="455" HorizontalAlignment="Left" Width="353">
<StackPanel>
<Label x:Name="labelCustomerName" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="16" FontWeight="Bold"/>
<Label x:Name="labelCustomerOrganization" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="16" />
<Label x:Name="labelCustomerStreet" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="16" />
<Label x:Name="labelCustomerSuburb" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="16" />
<Label x:Name="labelCustomerPostcode" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="16" />
</StackPanel>
</GroupBox>
<DataGrid x:Name="dataGridOrderItems" Margin="369,0,4,51" VerticalAlignment="Bottom" Height="405" RenderTransformOrigin="0.5,0.5" Grid.Row="1"
VerticalGridLinesBrush="LightGray" HorizontalGridLinesBrush="LightGray" AlternatingRowBackground="Beige" AlternationCount="2" SelectionMode="Single"
SelectionChanged="dataGridOutstandingOrders_SelectionChanged" AutoGenerateColumns="False" IsReadOnly="True" Grid.ColumnSpan="2">
<DataGrid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform AngleY="-0.146"/>
<RotateTransform/>
<TranslateTransform Y="-0.497"/>
</TransformGroup>
</DataGrid.RenderTransform>
<DataGrid.Columns>
<DataGridTextColumn Header="Resource Name" Binding="{Binding ResourceName}" />
<DataGridTextColumn Header="Quantity Ordered" Binding="{Binding Quantity}" />
<DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate, StringFormat=\{0:d\}}" />
<DataGridCheckBoxColumn Header="Dispatched Resource" Binding="{Binding IsChecked}" />
<DataGridTextColumn/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Ended up resolving this one myself - it looks like I didn't have both selection modes. Updated (working XAML):
<DataGrid x:Name="dataGridOrderItems" Margin="369,0,4,51" VerticalAlignment="Bottom" Height="405" Grid.Row="1"
VerticalGridLinesBrush="LightGray" HorizontalGridLinesBrush="LightGray" AlternatingRowBackground="Beige" AlternationCount="2"
SelectionMode="Single" SelectionUnit="FullRow"
AutoGenerateColumns="False" IsReadOnly="False" Grid.ColumnSpan="2">
<DataGrid.Columns>
<DataGridTextColumn Header="Resource Name" Binding="{Binding ResourceName}" />
<DataGridTextColumn Header="Quantity Ordered" Binding="{Binding Quantity}" />
<DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate, StringFormat=\{0:d\}}" />
<DataGridCheckBoxColumn Header="Dispatched Resource" Binding="{Binding IsChecked}" />
<DataGridTextColumn/>
</DataGrid.Columns>
</DataGrid>
I think your isReadOnly property cause this one. After you change isReadOnly to 'False' to solve it.
Saw this link which seems to be about the same issue:
There is no possibility to sort a date column in DataGrid
and the response there was "Try setting the SortMemberPath."
Unfortunately I don't know what to set it to! I followed the link to the property help page on MSDN but that did not clarify anything. Here's my xaml:
<sdk:DataGrid x:Name="dg_ReferralsTS" Margin="5,5,0,25" AutoGenerateColumns="False" CanUserSortColumns="True" Grid.Row="1" VerticalAlignment="Stretch" MaxHeight="{Binding ElementName=reportsRow1, Path=Height.Star, Mode=OneWay}" >
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn x:Name="show_On_ChartColumn" Header="Show On Chart" Width="SizeToHeader">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<CheckBox x:Name="chkBox_ShowReferrals" HorizontalAlignment="Center" Tag="{Binding Path=referral.File_Reference}" IsChecked="{Binding Path=isChecked, Mode=TwoWay}" Checked="chk_ShowReferral_Checked" Unchecked="chk_ShowReferral_Unchecked" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn x:Name="file_ReferenceColumn" SortMemberPath="referral.File_Reference" Binding="{Binding Path=referral.File_Reference, Mode=OneWay}" Header="File Reference" IsReadOnly="True" Width="SizeToHeader" />
<sdk:DataGridTemplateColumn x:Name="DateToday_DateColumn" Header="Date Today" Width="120" CanUserSort="True" SortMemberPath="referral.Date_Today">
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<sdk:DatePicker SelectedDate="{Binding Path=referral.Date_Today, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, TargetNullValue=''}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=referral.Date_Today, StringFormat=\{0:d\}}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn x:Name="latitudeColumn" Binding="{Binding Path=referral.Latitude}" Header="Latitude" Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="longitudeColumn" Binding="{Binding Path=referral.Longitude}" Header="Longitude" Width="SizeToHeader" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
Try replacing
SortMemberPath="referral.Date_Today"
With
SortMemberPath="Date Today"
When it's a DataGridTemplateColumn, you need to set the SortMemberPath and also it's mandatory to set CanUserSort property to true.
(i.e. CanUserSort="True" SortMemberPath="Date_Today")