binding to datepicker in datagrid rowdetails - c#

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}"

Related

Sorting and scrolling is very slow in DataGrid when using LINQ

Sorting and scrolling is very slow in DataGrid when using LINQ. It works great when binding it with results from stored procedure.
There are only 200 records. Why is that?
I have tried DataGrid with following properties but had no luck:
EnableRowVirtualization="True"
EnableColumnVirtualization="True"
ScrollViewer.CanContentScroll="True"
Here is the simple LINQ code which returns a list of customers with customer object:
public List<Customer> GetLinkedCustomer()
{
using (MYModel context = new MYModel())
{
return context.Customers.Where(x => x.CustLong1 != null).OrderBy(customer => customer.LastName).ToList();
}
}
XAML code:
<Grid Margin="5,5,5,5" Height="Auto" Width="700">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" Name="dgLinkedCustomers" Height="250" CanUserAddRows="False" AutoGenerateColumns="False" CanUserSortColumns="True" IsReadOnly ="True" SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding FirstName , IsAsync=True}" Header="{x:Static res:Strings.Common_FirstName}" Width="150"/>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding LastName , IsAsync=True}" Header="{x:Static res:Strings.Common_LastName}" Width="175"/>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding ShopName, IsAsync=True}" Header="{x:Static res:Strings.ShopName}" Width="215"/>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding CustLong1 , IsAsync=True}" Header="{x:Static res:Strings.AccountNumber}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
<StackPanel Grid.Row="1" Orientation="Vertical" Margin="0,0,0,5" HorizontalAlignment="Right">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Button Name="Close" Margin="0,0,10,0" Width="100">
<AccessText Text="{x:Static res:Strings.BtnText_Close}" />
</Button>
<Button Name="Unlink" Margin="0,0,10,0" Width="100" Click="Unlink_OnClick">
<AccessText>Unlink</AccessText>
</Button>
</StackPanel>
</StackPanel>
</Grid>

WPF InitializeComponent is not recognized

Completely new to WPF and following a Microsoft walkthrough which asked to replace the XML with the following:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Northwind Orders" Height="335" Width="425"
Name="OrdersWindow" Loaded="Window1_Loaded">
<Grid Name="orderItemsGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="258*"/>
<ColumnDefinition Width="161*"/>
</Grid.ColumnDefinitions>
<ComboBox DisplayMemberPath="OrderID" ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="true"
Height="23" Margin="92,12,37.703,0" Name="comboBoxOrder" VerticalAlignment="Top"/>
<DataGrid ItemsSource="{Binding Path=Order_Details}"
CanUserAddRows="False" CanUserDeleteRows="False"
Name="orderItemsDataGrid" Margin="34,46,34.4,49.6"
AutoGenerateColumns="False" Grid.ColumnSpan="2">
<DataGrid.Columns>
<DataGridTextColumn Header="Product" Binding="{Binding ProductID, Mode=OneWay}" />
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity, Mode=TwoWay}" />
<DataGridTextColumn Header="Price" Binding="{Binding UnitPrice, Mode=TwoWay}" />
<DataGridTextColumn Header="Discount" Binding="{Binding Discount, Mode=TwoWay}" />
</DataGrid.Columns>
</DataGrid>
<Label Height="28" Margin="34,12,0,0" Name="orderLabel" VerticalAlignment="Top"
HorizontalAlignment="Left" Width="65">Order:</Label>
<StackPanel Name="Buttons" Orientation="Horizontal" HorizontalAlignment="Right"
Height="40" Margin="0,261,22.4,4.6" Grid.ColumnSpan="2">
<Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,12"
Name="buttonSave" VerticalAlignment="Bottom" Width="75"
Click="buttonSaveChanges_Click">Save Changes
</Button>
<Button Height="23" Margin="0,0,12,12"
Name="buttonClose" VerticalAlignment="Bottom" Width="75"
Click="buttonClose_Click">Close</Button>
</StackPanel>
</Grid>
</Window>
So I replaced it but now if I go to the file MainWindow.xmal.cs the code for InitializeComponent(); is being highlighted as not existing.
Why is this and how do I correct it?
You must match the x:Class="Window1" in XAML with code behind class class MainWindow.
Change the XAML from x:Class="Window1" to x:Class="NorthwindClient.MainWindow"

binding datagrid column to selecteditem property

I have a datagrid where if a row is clicked upon the row details is shown. In the row details is another datagrid called dgRights.
So dgRights is bound to SelectItem.Funds where funds is a custom list. dgRights show 4 columns, 3 of which are bound fine however the fourth isn't (in my code below its called "Rights Sedol").
I want the column Rights Sedol to be bound to a property of the selectedItem not selectedItem.Funds, is this possible?
I've tried the lines of code below without luck,
<DataGridTextColumn Header="Rights Sedol" Binding="{Binding SelectedItem.NewSecurity.Sedol, RelativeSource={RelativeSource AncestorType=Window}}/>
2nd
<DataGridTextColumn Header="Rights Sedol" Binding="{Binding SelectedItem.NewSecurity.Sedol, RelativeSource={RelativeSource AncestorType=DataGrid}}/>
I also tried changing the ItemSource of the datagrid from SelectedItem.Funds to just SelectItem & changed the other 3 working columns to Funds.Code etc but this didn't display any data in the datagrid at all. So not sure what to do?
App.xaml - my row details data template
<DataTemplate x:Key="DG_RowDetailRGHTSHist">
<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="200"/>
<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="Fund Summary">
</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="Rights Sedol" Binding="{Binding SelectedItem.NewSecurity.Sedol, RelativeSource={RelativeSource AncestorType=Window}}" IsReadOnly="True" MinWidth="75"/>
<DataGridTextColumn Header="Number of Rights" Binding="{Binding CurrentNominal, Mode=TwoWay, StringFormat={}{0:N0}}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Border>
</Grid>
</DataTemplate>
You want to access the 2nd datagrid up so you need to specify AncestorLevel=2.
{RelativeSource AncestorType=DataGrid, AncestorLevel=2}}

Why can't I select cells in my WPF datagrid?

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.

How to remove ControlTemplate from a control

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

Categories

Resources