WPF InitializeComponent is not recognized - c#

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"

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>

How to enable a datagrid column text in C#/ wpf

I have an app in which as soon as I click the "Insert" I want the program to enable the text in data grid and the user can enter the information of the product. Can someone help me with how can I do this?
below the app code and picture
enter image description here
<Window>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" FontWeight="Bold" HorizontalAlignment="Center">Produtos</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" FontWeight="Bold" HorizontalAlignment="Center">Unidades</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="2" FontWeight="Bold" HorizontalAlignment="Center">Empresas</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="3" FontWeight="Bold" HorizontalAlignment="Center">Operadores</TextBlock>
<DataGrid Grid.Row="1" Grid.Column="0" CanUserAddRows="False" CanUserDeleteRows="False" Margin="5" ItemsSource="{Binding ListaProdutos}" AutoGenerateColumns="False" SelectedItem="{Binding ProdutoSelecionado}">
<DataGrid.Columns>
<DataGridTextColumn Header="Descrição" Binding="{Binding Descricao, UpdateSourceTrigger=PropertyChanged}" Width="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<DataGrid Grid.Row="1" Grid.Column="1" CanUserAddRows="False" CanUserDeleteRows="False" Margin="5" ItemsSource="{Binding ListaUnidades}" AutoGenerateColumns="False" SelectedItem="{Binding UnidadeSelecionada}">
<DataGrid.Columns>
<DataGridTextColumn Header="Descrição" Binding="{Binding Descricao, UpdateSourceTrigger=PropertyChanged}" Width="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<DataGrid Grid.Row="1" Grid.Column="2" CanUserAddRows="False" CanUserDeleteRows="False" Margin="5" ItemsSource="{Binding ListaEmpresas}" AutoGenerateColumns="False" SelectedItem="{Binding EmpresaSelecionada}">
<DataGrid.Columns>
<DataGridTextColumn Header="Descrição" Binding="{Binding Descricao, UpdateSourceTrigger=PropertyChanged}" Width="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<DataGrid Grid.Row="1" Grid.Column="3" CanUserAddRows="False" CanUserDeleteRows="False" Margin="5" ItemsSource="{Binding ListaOperadores}" AutoGenerateColumns="False" SelectedItem="{Binding OperadorSelecionado}">
<DataGrid.Columns>
<DataGridTextColumn Header="Nome" Binding="{Binding Nome, UpdateSourceTrigger=PropertyChanged}" Width="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<StackPanel Grid.Row="2" Grid.Column="0" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Command="{Binding InserirProdutoCommand}" Width="50" Height="25">Inserir</Button>
<Button Command="{Binding ExcluirProdutoCommand}" Width="50" Height="25">Excluir</Button>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Command="{Binding InserirUnidadeCommand}" Width="50" Height="25">Inserir</Button>
<Button Command="{Binding ExcluirUnidadeCommand}" Width="50" Height="25">Excluir</Button>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Command="{Binding InserirEmpresaCommand}" Width="50" Height="25">Inserir</Button>
<Button Command="{Binding ExcluirEmpresaCommand}" Width="50" Height="25">Excluir</Button>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="3" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Command="{Binding InserirOperadorCommand}" Width="50" Height="25" HorizontalAlignment="Center">Inserir</Button>
<Button Command="{Binding ExcluirOperadorCommand}" Width="50" Height="25" HorizontalAlignment="Center">Excluir</Button>
</StackPanel>
<Border Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="4" BorderThickness="0 1 0 0" BorderBrush="Gray" Margin="10,10,5,0" Padding="5 5 5 0" VerticalAlignment="Top" Height="46">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Command="{Binding SalvarCommand}" Width="80" Height="30">Salvar</Button>
<Button Command="{Binding CancelarCommand}" Width="80" Height="30">Cancelar</Button>
</StackPanel>
</Border>
</Grid>
</Window>
I found a solution to meet the require,but it needs object bound to Datagrid has a boolean property.Here's the xaml:
<DataGridTemplateColumn Width="120">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox
Name="innerTextBox"
BorderThickness="0"
IsReadOnly="{Binding DisableEdit, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding Name}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
and here is the object:
public class Person:BindableBase
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private bool disableEdit;
public bool DisableEdit
{
get { return disableEdit; }
set { SetProperty(ref disableEdit, value); }
}
}
And here is button_click event:
var person = datagrid.SelectedCells[0].Item as Person;
person.DisableEdit = false;
Put UpdateSourceTrigger=PropertyChanged into DataGrid binding instead of DataGridTextColumn

How can I make a ComboBox in a DataGrid row unique?

Environment for the following question:
C#, WPF, XAML
How can I achieve to setup a table/a DataGrid with an undefined number of rows having a Combobox in an arbitrary column (number of columns is undefined as well)? (The other row's cells will be filled with the properties of the underyling object/entity.) The ComboBoxes shall all have the same items but the selected item shall be assigned to the underlying object of the row (of course). Hence, I think the ComboBox of each row must have a unique identifier.
The selected item shall be stored in a property of the underlying object/entity.
By the way:
The ComboBox shall be filled with items of a collection (List) that is not part of the row's underlying object/entity.
What would be the best way using WPF / XAML?
UPDATE (2018-12-14):
<Window x:Class="ConfigTool.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:ConfigTool"
xmlns:lb="clr-namespace:ConfigTool.DataBinding"
mc:Ignorable="d"
Title="xxx" Height="650" Width="1200" Closing="CloseWindow">
<Window.Resources>
<CollectionViewSource x:Key="TagsCollectionViewSource" CollectionViewType="ListCollectionView"/>
<CollectionViewSource x:Key="NotificationsCollectionViewSource" CollectionViewType="ListCollectionView"/>
<CollectionViewSource x:Key="TagNamesCollectionViewSource" CollectionViewType="ListCollectionView"/>
<lb:StringListConverter x:Key="StringListConverter" />
</Window.Resources>
<Grid Margin="10,10,10,10">
<TabControl>
<TabItem Header="Tags">
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<DataGrid x:Name="tagsGrid" DataContext="{StaticResource TagsCollectionViewSource}" ItemsSource="{Binding}"
AlternatingRowBackground="LightBlue" AutoGenerateColumns="False" CanUserAddRows="True" IsReadOnly="False"
SelectionMode="Single" BorderBrush="Magenta" BorderThickness="3">
<DataGrid.Columns>
<DataGridTextColumn x:Name="TagName" Header="Tag name" Binding="{Binding Mode=TwoWay, Path=TagName}"></DataGridTextColumn>
<DataGridTextColumn Header="Cycle" Binding="{Binding Mode=TwoWay, Path=Cycle}"></DataGridTextColumn>
<DataGridTextColumn Header="Source" Binding="{Binding Mode=TwoWay, Path=Source}"></DataGridTextColumn>
<DataGridTemplateColumn x:Name="editTagColumn" Header="Edit">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<WrapPanel>
<Button x:Name="btnTagDelete" Click="BtnTagDelete_Click" CommandParameter="{Binding}" Height="15" Width="15" Margin="2">
<Button.Content>
<Image Source="Resources/delete.png"></Image>
</Button.Content>
</Button>
</WrapPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
</TabItem>
<TabItem Header="Notifications">
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<DataGrid x:Name="notificationsGrid" DataContext="{StaticResource NotificationsCollectionViewSource}" ItemsSource="{Binding}"
AlternatingRowBackground="LightBlue" AutoGenerateColumns="False" CanUserAddRows="True" IsReadOnly="False"
SelectionMode="Single" BorderBrush="Magenta" BorderThickness="3">
<DataGrid.Columns>
<!--<DataGridTextColumn Header="Tag name" Binding="{Binding Mode=TwoWay, Path=TagName}"></DataGridTextColumn>-->
<DataGridTemplateColumn x:Name="tagNameColumn" Header="Tag name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<!--<ItemContainerTemplate>
<StackPanel>-->
<!--DataContext="{Binding Source={StaticResource TagNamesCollectionViewSource}}">-->
<!--<ComboBox Width="200" DataContext="{StaticResource TagNamesCollectionViewSource}" ItemsSource="{Binding TagNames, Converter={StaticResource StringListConverter}}">-->
<ComboBox Name="notificationTagName" Width="200" DataContext="{StaticResource TagNamesCollectionViewSource}" ItemsSource="{Binding Source={StaticResource TagNamesCollectionViewSource}, RelativeSource={RelativeSource AncestorType=local:MainWindow}}"
SelectionChanged="notificationTagName_SelectionChanged" />
<!--<ComboBox Width="200" DataContext="{StaticResource TagNamesCollectionViewSource}" ItemsSource="{Binding Converter={StaticResource StringListConverter}}" />-->
<!--<ComboBox Width="200" ItemsSource="{Binding ElementName=Window2, Path=DataContext.TagNames, Converter={StaticResource StringListConverter}}" />-->
<!--<ComboBox HorizontalAlignment="Left" Margin="256,260,0,0" VerticalAlignment="Top" Width="120" x:Name="DataList" ItemsSource="{Binding DetailParams, Converter={StaticResource StringListConverter}}"/>-->
</DataTemplate>
<!--</StackPanel>
</ItemContainerTemplate>-->
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridCheckBoxColumn Header="IsActive" Binding="{Binding Mode=TwoWay, Path=IsActive}"></DataGridCheckBoxColumn>
<DataGridTextColumn Header="Type" Binding="{Binding Mode=TwoWay, Path=Type}"></DataGridTextColumn>
<DataGridTextColumn Header="Limit" Binding="{Binding Mode=TwoWay, Path=Limit}"></DataGridTextColumn>
<DataGridTextColumn Header="DetailTemplate" Binding="{Binding Mode=TwoWay, Path=DetailTemplate}"></DataGridTextColumn>
<!--<DataGridTextColumn Header="DetailParams" Binding="{Binding Mode=TwoWay, Path=DetailParams, Converter={StaticResource StringListConverter}}"></DataGridTextColumn>-->
<DataGridTemplateColumn x:Name="detailsParamColumn" Header="Edit">
<DataGridTemplateColumn.CellTemplate>
<ItemContainerTemplate>
<StackPanel> <!--DataContext="{Binding Source={StaticResource TagNamesCollectionViewSource}}">-->
<!--<ComboBox Width="200" DataContext="{StaticResource TagNamesCollectionViewSource}" ItemsSource="{Binding TagNames, Converter={StaticResource StringListConverter}}">-->
<ComboBox Width="200" DataContext="{StaticResource TagNamesCollectionViewSource}" ItemsSource="{Binding}" />
<!--<ComboBox Width="200" DataContext="{StaticResource TagNamesCollectionViewSource}" ItemsSource="{Binding Converter={StaticResource StringListConverter}}" />-->
<!--<ComboBox Width="200" ItemsSource="{Binding ElementName=Window2, Path=DataContext.TagNames, Converter={StaticResource StringListConverter}}" />-->
<!--<ComboBox HorizontalAlignment="Left" Margin="256,260,0,0" VerticalAlignment="Top" Width="120" x:Name="DataList" ItemsSource="{Binding DetailParams, Converter={StaticResource StringListConverter}}"/>-->
</StackPanel>
</ItemContainerTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="CauseProbability" Binding="{Binding Mode=TwoWay, Path=CauseProbability, Converter={StaticResource StringListConverter}}"></DataGridTextColumn>
<DataGridTemplateColumn x:Name="editNotificationsColumn" Header="Edit">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<WrapPanel>
<Button x:Name="btnNotificationDelete" Click="BtnNotificationDelete_Click" Height="15" Width="15" Margin="2">
<Image Source="Resources/delete.png"></Image>
</Button>
</WrapPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
</TabItem>
</TabControl>
</Grid>
</Window>
It is happening because you are using the same collection for the Combo-Box in each row.
The ItemsSource for the Combo-Box should be a collection and it should be a member/property of class that set as DataContext for the DataGrid.
Or
Bind the SelectedItem property of Combo-Box to a member of your class.
See https://www.c-sharpcorner.com/uploadfile/dpatra/combobox-in-datagrid-in-wpf/

binding to datepicker in datagrid rowdetails

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

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