I have created a small app, for which View part is as below.
I have used ListView for showing my data. This table (ListView) is being updated once I enter the data and click on Save. After which if I click on the data, the whole row is selected.
But I am not sure how to click an individual cell.
Can I do it using ListView? If Yes how? Or I need some other component?
VehicalForm.xaml
<Window x:Class="Seris.VehicalForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="600">
<WrapPanel Orientation="Vertical" Margin="10 " >
<Label Content="Vehical No" HorizontalAlignment="Left"/>
<TextBox Name="VehicalNo_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding VehicalNo}" HorizontalAlignment="Left" />
<Label Content="Model" HorizontalAlignment="Left"/>
<TextBox Name="Model_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding Model}" HorizontalAlignment="Left" />
<Label Content="Manufacturing Date" HorizontalAlignment="Left"/>
<DatePicker Name="ManufacturingDate_DateTime" SelectedDate="{Binding ManufacturingDate, Mode=TwoWay}"/>
<Label Content="IU No" HorizontalAlignment="Left"/>
<TextBox Height="23" Width="80" Name="IUNO_Text" TextWrapping="Wrap" Text="{Binding IUNo}" HorizontalAlignment="Left"/>
<Label Content="Personnel" HorizontalAlignment="Left"/>
<ComboBox Name="Personnel_Combo" Text="{Binding Personnel}" HorizontalAlignment="Left" Width="116"/>
<Separator Height="20" RenderTransformOrigin="0.5,0.5" Width="16"/>
<Button Name="Save_Button" Command="{Binding SaveButton_Command}" Content="Save" Width="66"/>
<Label x:Name="Error_Label" Content="{Binding ErrorMessage, UpdateSourceTrigger=PropertyChanged}" Foreground="Red" HorizontalAlignment="Left" Height="41" Width="137"/>
<ListView Height="294" Width="371" ItemsSource="{Binding ListItems, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" >
<ListView.View>
<GridView>
<GridViewColumn Header="Vehical No" DisplayMemberBinding="{Binding VehicalNo}" />
<GridViewColumn Header="Model" DisplayMemberBinding="{Binding Model}" />
<GridViewColumn Header="ManufacturingDate" DisplayMemberBinding="{Binding ManufacturingDate}" />
<GridViewColumn Header="IUNo" DisplayMemberBinding="{Binding IUNo}" />
<GridViewColumn Header="Personnel" DisplayMemberBinding="{Binding Personnel}" />
</GridView>
</ListView.View>
</ListView>
</WrapPanel>
</Window>
You can achieve this with a DataGrid. Replace your listview with something like this;
<DataGrid Height="294" Width="371" ItemsSource="{Binding ListItems, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" SelectionUnit="Cell" SelectionMode="Single" AutoGenerateColumns="False">
<DataGridTextColumn Header="Vehical No" Binding="{Binding VehicalNo}" />
<DataGridTextColumn Header="Model" Binding="{Binding Model}" />
<DataGridTextColumn Header="ManufacturingDate" Binding="{Binding ManufacturingDate}" />
<DataGridTextColumn Header="IUNo" Binding="{Binding IUNo}" />
<DataGridTextColumn Header="Personnel" Binding="{Binding Personnel}" />
</DataGrid>
Two important properties of the DataGrid are;
SelectionUnit="Cell" SelectionMode="Single"
These allow you to control how a user can select items. In this example you can only select a single cell.
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}" />
?
hello i have data in a gridView and i want to change the size of the data:
i mean i want to resize the data who comes from a list in c#
code:
<Window x:Class="DadBussines.PL.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="800" Width="1600">
<Grid>
<Label Content="product name" FontSize="25" Margin="705,121,652.6,590.4"/>
<TextBox Name="name_textBox" FontSize="25" HorizontalAlignment="Left" Height="58" Margin="599,195,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="400" AllowDrop="True" IsHitTestVisible="True" TextChanged="name_textBox_TextChanged" />
<Button Content="add product" FontSize="25" HorizontalAlignment="Left" Margin="19,27,0,0" VerticalAlignment="Top" Width="123" Click="Button_Click_Add_Product"/>
<Button Content="delete product" FontSize="25" HorizontalAlignment="Left" Margin="161,27,0,0" VerticalAlignment="Top" Width="129" Click="Button_Click_Delete_Product"/>
<Button Content="add amount" FontSize="25" HorizontalAlignment="Left" Margin="304,27,0,0" VerticalAlignment="Top" Width="142" Click="Button_Click_Add_Amount"/>
<Button Content="delete amount" FontSize="25" HorizontalAlignment="Left" Margin="461,27,0,0" VerticalAlignment="Top" Width="151" Click="Button_Click_Remove_Amount"/>
<Button Content="change price" FontSize="25" HorizontalAlignment="Left" Margin="629,27,0,0" VerticalAlignment="Top" Width="151" Click="Button_Click_Change_Price" />
<Label Content="money in staff" FontSize="25" HorizontalAlignment="Left" Margin="1388,90,0,0" VerticalAlignment="Top" Width="166"/>
<Label Name="sumOfMoney_label" Content="" FontSize="25" HorizontalAlignment="Left" Margin="1185,95,0,0" VerticalAlignment="Top" Width="177"/>
<ListView Margin="10,288,10,10" Name="products_ListView" >
<ListView.View>
<GridView>
<GridViewColumn Header="index" Width="40" DisplayMemberBinding="{Binding id}"/>
<GridViewColumn Header="name product" Width="312" DisplayMemberBinding="{Binding name}"/>
<GridViewColumn Header="price product" Width="312" DisplayMemberBinding="{Binding price}" />
<GridViewColumn Header="amount product in cartons" Width="312" DisplayMemberBinding="{Binding amount}" />
<GridViewColumn Header="amount product inside cartons" Width="312" DisplayMemberBinding="{Binding amount_in_box}" />
<GridViewColumn Header="product type" Width="312" DisplayMemberBinding="{Binding catagory}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
If you want to style the output of each gridviewcolumn you need to set the celltemplate property of the column to something.
Take a look at the sample on MSDN:
http://msdn.microsoft.com/en-us/library/system.windows.controls.gridviewcolumn.celltemplate(v=vs.110).aspx
I have designed a wpf page. But I not able to set Proper Tab Navigation on Grid view. Controls (grid view) on the page are not following tab index. Page contain Grid and Save,Cancel button.
There is a gridview. This grid has rows and columns. Each row contains 2 autocompletebox and 6 textboxes. When first i enter the value on the first autocompletebox,then enter tab it move to next box and so on. I enter the value in last text box and press enter button, then a new row will be formed in the grid. Then i press the tab it focus move on the outside button(Save button).
I want to move the focus on the next box( first autocomplete box,not on the save button) in the second row in the grid.Pls help...
XAML
<GridView KeyboardNavigation.IsTabStop="False" >
<GridViewColumn Header="Id" Width="0" DisplayMemberBinding="{Binding Path=PurchaseItemId}"></GridViewColumn>
<GridViewColumn Header="No." Width="20" DisplayMemberBinding="{Binding Path=No, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"></GridViewColumn>
<GridViewColumn Header="ItemId" Width="0" DisplayMemberBinding="{Binding Path=ItemId}"></GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource hcs}" Header="Item Code" Width="60">
<GridViewColumn.CellTemplate >
<DataTemplate>
<StackPanel>
<!--<TextBox x:Name="txtItemCode" Width="60" Text="{Binding Path=ItemCode, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" TabIndex="2" />-->
<my:AutoCompleteBox Canvas.Left="295" Canvas.Top="393" Name="txtItemCode" Text="{Binding Path=ItemCode, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" ItemsSource="{Binding Path=AutoCompleteBoxCodes, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Path=AutoCompleteBoxSelectedCode1, NotifyOnValidationError=True, ValidatesOnDataErrors=True,UpdateSourceTrigger=LostFocus, Mode=TwoWay}" IsTextCompletionEnabled="True" TextChanged="txtItemCode_TextChanged" KeyDown="txtItemCode_KeyDown" SelectionChanged="txtItemCode_SelectionChanged" />
<!--<ListBox x:Name="lstItemCodes" Width="250" Height="100" ItemsSource="{Binding Path=PurchaseItemCodes}" Visibility="{Binding Path=IsItemCodeListVisible}" SelectedItem="{Binding Path=SelectedItemCode1, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Code}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>-->
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource hcs}" Header="Item Name" Width="120">
<GridViewColumn.CellTemplate >
<DataTemplate>
<StackPanel>
<my:AutoCompleteBox Canvas.Left="295" Canvas.Top="393" Name="txtItemName" Text="{Binding Path=ItemName, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" ItemsSource="{Binding Path=AutoCompleteBoxNames, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Path=AutoCompleteBoxSelectedName1, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" IsTextCompletionEnabled="True" Width="100" TextChanged="txtItemName_TextChanged" PreviewKeyDown="txtItemName_PreviewKeyDown" IsHitTestVisible="True" />
<!--<TextBox Name="txtItemName" Width="120" Text="{Binding Path=ItemName, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>
<ListBox x:Name="lstItemNames" Width="250" Height="70" ItemsSource="{Binding Path=PurchaseItems}" Visibility="{Binding Path=IsItemListVisible}" SelectedItem="{Binding Path=SelectedItem1, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Bottom" AllowDrop="False">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=Name}" FontWeight="ExtraBlack" />
<TextBlock Text="{Binding Path=Category.CategoryName}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>-->
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource hcs}" Header="Qty" Width="50" >
<GridViewColumn.CellTemplate >
<DataTemplate>
<TextBox x:Name="txtQuantity" Width="45" Text="{Binding Path=Quantity, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" TextChanged="txtQuantity_TextChanged" PreviewTextInput="txtQuantity_PreviewTextInput" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource hcs}" Header="Purchase Price" Width="80">
<GridViewColumn.CellTemplate >
<DataTemplate>
<TextBox x:Name="txtPurchasePrice" Width="60" Text="{Binding Path=Purchaseprice, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" PreviewTextInput="txtPurchasePrice_PreviewTextInput" TextChanged="txtPurchasePrice_TextChanged" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource hcs}" Header="Unit" Width="70">
<GridViewColumn.CellTemplate >
<DataTemplate>
<ComboBox Canvas.Left="10" Canvas.Top="10" Height="23" Name="cmbUnit" Width="55" ItemsSource="{StaticResource UnitData }" SelectedItem="{Binding Path=Unit}">
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource hcs}" Header="ProfitAmount" Width="75">
<GridViewColumn.CellTemplate >
<DataTemplate>
<TextBox Name="txtProfitAmount" Width="70" Text="{Binding Path=ProfitAmount, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" TextChanged="txtProfitAmount_TextChanged" PreviewTextInput="txtProfitAmount_PreviewTextInput" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource hcs}" Header="Profit %" DisplayMemberBinding="{Binding Path=ProfitPercent, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Width="50">
<!--<GridViewColumn.CellTemplate >
<DataTemplate>
<TextBox Name="txtProfitPercent" Width="80" Text="{Binding Path=ProfitPercent, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" IsEnabled="False" />
</DataTemplate>
</GridViewColumn.CellTemplate>-->
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource hcs}" Header="Selling Price" DisplayMemberBinding="{Binding Path=SellingPrice, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Width="70">
<!--<GridViewColumn.CellTemplate >
<DataTemplate>
<TextBox Name="txtSellingPrice" Width="100" Text="{Binding Path=SellingPrice, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" IsEnabled="False" />
</DataTemplate>
</GridViewColumn.CellTemplate>-->
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource hcs}" Header="Tax" Width="40">
<GridViewColumn.CellTemplate >
<DataTemplate>
<TextBox Name="txtTax" Width="35" Text="{Binding Path=Tax, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" TextChanged="txtTax_TextChanged" PreviewTextInput="txtTax_PreviewTextInput" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource hcs}" Header="Description" Width="80">
<GridViewColumn.CellTemplate >
<DataTemplate>
<TextBox Name="txtDescription" Width="75" Text="{Binding Path=Description, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource hcs}" Header="ShelfLocation" Width="100">
<GridViewColumn.CellTemplate >
<DataTemplate>
<TextBox Name="txtShelfLocation" Width="120" Text="{Binding Path=ShelfLocation, NotifyOnValidationError=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" KeyDown="txtShelfLocation_KeyDown" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Total" Width="80" DisplayMemberBinding="{Binding Path=Total, UpdateSourceTrigger=PropertyChanged}"></GridViewColumn>
</GridView >
Thank you...
If you want TabNavigation to be cycle within the GridView and shouldn't leave the GridView, set KeyboardNavigation.TabNavigation="Cycle" on your GridView.
Possible enumeration modes for KeyboardNavigation.TabNavigation attached property can be found here.
For reference:
Set the IsTabStop=false for the buttons.
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
I have a collection of items where the last column is a collection with a celltemplate of it's own.
The ListView's item source is set dynamically in code and all of the other columns are rendering correctly. However, the final column's data is not being read at all. I've tried different solutions from other questions, but they don't seem to work for me.
<Window x:Class="bbowl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Title="Blood Bowl Data" Height="350" Width="525">
<Window.Resources>
<DataTemplate x:Key="SkillTemplate">
<TextBlock Text="{Binding Path=name}" ToolTip="{Binding Path=description}" Height="18" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="Blue"><TextBlock.TextDecorations><TextDecoration /></TextBlock.TextDecorations></TextBlock>
</DataTemplate>
<DataTemplate x:Key="SkillsTemplate">
<DataGrid AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" HeadersVisibility="None" HorizontalScrollBarVisibility="Hidden" ItemsSource="{Binding Path=Skills}" BorderThickness="0" ItemTemplate="{StaticResource SkillTemplate}" />
</DataTemplate>
</Window.Resources>
<Grid>
<ComboBox HorizontalAlignment="Left" Name="cbxRace" VerticalAlignment="Top" Width="503" Height="25" SelectionChanged="cbxRace_SelectionChanged" ItemsSource="{Binding}" />
<Label Content="Rerolls:" Height="25" HorizontalAlignment="Left" Margin="0,25,0,0" Name="lblRerollLabel" VerticalAlignment="Top" Width="435" HorizontalContentAlignment="Right" FontWeight="Bold" />
<Label Content="0" Height="25" HorizontalAlignment="Left" Margin="441,25,0,0" Name="lblReroll" VerticalAlignment="Top" Width="62" HorizontalContentAlignment="Right" />
<ListView Height="263" HorizontalAlignment="Left" Margin="0,48,0,0" Name="lvwPlayer" VerticalAlignment="Top" Width="503" ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=name}"/>
<GridViewColumn Header="MA" DisplayMemberBinding="{Binding Path=ma}" />
<GridViewColumn Header="ST" DisplayMemberBinding="{Binding Path=st}" />
<GridViewColumn Header="AG" DisplayMemberBinding="{Binding Path=ag}" />
<GridViewColumn Header="Max" DisplayMemberBinding="{Binding Path=max}" />
<GridViewColumn Header="Price" DisplayMemberBinding="{Binding Path=price}" />
<GridViewColumn Header="Skills" CellTemplate="{StaticResource SkillsTemplate}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
To solve your problem you would need to change your SkillsTemplate to something like this:
<DataTemplate x:Key="SkillsTemplate">
<DataGrid
AutoGenerateColumns="False"
HorizontalAlignment="Left"
VerticalAlignment="Top"
HeadersVisibility="None"
ItemsSource="{Binding Path=Skills}"
BorderThickness="0"
GridLinesVisibility="None">
<DataGrid.Columns>
<DataGridTemplateColumn CellTemplate="{StaticResource SkillTemplate}"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
which instead of changing whole items template creates one column that displays your text using SkillTemplate. However, as far I can see, for what you want to do with it you may consider using less complicated control like ListBox or even ItemsControl when you just want to display bunch of items without giving user option to select one
<DataTemplate x:Key="SkillsTemplate">
<ItemsControl ItemsSource="{Binding Path=Skills}" ItemTemplate="{StaticResource SkillTemplate}"/>
</DataTemplate>