I have an expander containing a datagrid with a rowdetailstemplate.
When I click on a datagrid row to see the row details template, the expander closes!
Anyone know why and how to fix this?
Update
Turns out that something else is happening:
The expanders are all contained in a TabItem. When I click on the grid row, TabControls.SelectionChanged fires (WHY!!!?!?!!?!?!?!) and then the data is recalculated and rebound.
So the real question is why does click a DataGrid row cause the Parent TabControl's SelectionChanged Event to fire? Here's the XAML:
<Window xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" x:Class="BPPDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BPPDemo"
Title="BPP Demo" Height="600" Width="700" Loaded="Window_Loaded">
<Window.Resources>
<local:AddrStatusColorConverter x:Key="addressStatusColorConverter"/>
<local:BoolToVisConverter x:Key="boolToVisConverter"/>
</Window.Resources>
<DockPanel>
<Menu DockPanel.Dock="Top">
<Menu.Items>
<MenuItem Header="File">
<MenuItem Header="Open Assessment" Command="{Binding SelectAssessmentCommand}"></MenuItem>
<MenuItem Header="Import Properties" Command="{Binding ImportPropertiesCommand}"></MenuItem>
</MenuItem>
<MenuItem Header="Properties" Visibility="{Binding IsSelected, ElementName=tabProperties,Converter={StaticResource boolToVisConverter}}">
<MenuItem Header="Delete All Properties" Command="{Binding DeleteAllPropertiesCommand}"></MenuItem>
<MenuItem Header="Resolve Invalid Addresses" Command="{Binding ValidateAddressesCommand}"></MenuItem>
<MenuItem Header="Select Situs" Command="{Binding SelectSitusCommand}"/>
</MenuItem>
</Menu.Items>
</Menu>
<DockPanel Margin="10">
<TextBlock Text="{Binding Assessment.Name}" FontWeight="Normal" FontSize="24" Foreground="#FF5C9EB7" DockPanel.Dock="Top" Margin="0,0,0,10"/>
<TabControl SelectionChanged="TabControl_SelectionChanged">
<TabItem x:Name="tabProperties" Header="Properties">
<DataGrid ItemsSource="{Binding Properties}" AutoGenerateColumns="False" IsReadOnly="True" SelectedItem="{Binding SelectedProperty,Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTextColumn Header="Description" Binding="{Binding Description}"/>
<DataGridTextColumn Header="Cost" Binding="{Binding Cost,StringFormat='{}{0:c}'}"/>
<DataGridTextColumn Header="Year Acquired" Binding="{Binding YearAcquired}"/>
<DataGridTextColumn Header="Life In Years" Binding="{Binding LifeInYears}"/>
<DataGridTextColumn Header="Schedule" Binding="{Binding Schedule}"/>
<DataGridTextColumn Header="Current Value" Binding="{Binding CurrentValue,StringFormat='{}{0:c}'}" />
<DataGridTextColumn Header="Tax" Binding="{Binding Tax,StringFormat='{}{0:c}'}" />
<DataGridTemplateColumn Header="Address">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Address}" Foreground="{Binding AddressStatus, Converter={StaticResource addressStatusColorConverter}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Address Status" Binding="{Binding AddressStatus}"/>
<DataGridTextColumn Header="Situs" Binding="{Binding Situs}"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock>THIS ONE WORKS! WHY DOESN'T THE OTHER ONE????</TextBlock>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</TabItem>
<TabItem Header="Listings" Name="tabListings">
<DataGrid x:Name="SitusGroups" AutoGenerateColumns="false" SelectionMode="Single" SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTextColumn Header="Situs" Binding="{Binding Situs,StringFormat='{}{0:c}',TargetNullValue='none'}"></DataGridTextColumn>
<DataGridTextColumn Header="Cost" Binding="{Binding Cost,StringFormat='{}{0:c}'}"></DataGridTextColumn>
<DataGridTextColumn Header="Value" Binding="{Binding Value,StringFormat='{}{0:c}'}"></DataGridTextColumn>
<DataGridTextColumn Header="Tax" Binding="{Binding Tax,StringFormat='{}{0:c}'}"></DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock>I JUST WANT TO SEE THIS WHEN I CLICK ON A ROW!</TextBlock>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</TabItem>
</TabControl>
</DockPanel>
</DockPanel>
The problem has to do with the way events are routed. TabControl and DataGrid derive from the same class, so when DataGrid SelectionChanged, my TabControl SelectionChanged handler was firing.
As the last comment at http://www.trentfguidry.net/post/2009/06/13/WPF-TabControl-SelectionChanged.aspx suggests, I checked the original source in my event handler and returned if the reference wasn't my tabcontrol.
Related
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/
I am building a WPF app where it includes an action of adding an item to a shopping cart. A DataGrid defined as follows is bind to a collection of Products, and a Button is present for each row. Now, when a Button is clicked, how do I know which Product object it is for?
<DataGrid Grid.Row="1" ItemsSource="{Binding }" IsReadOnly="True" IsSynchronizedWithCurrentItem="True"
AutoGenerateColumns="False" TextBlock.FontSize="20" CanUserSortColumns="True" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Header 1" Binding="{Binding ID}" Width="2*" SortMemberPath="{Binding ID}"/>
<DataGridTextColumn Header="Header 2" Binding="{Binding Name}" Width="2*" SortMemberPath="{Binding Name}"/>
<DataGridTemplateColumn Header="Add To Cart" Width="2*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Click To Add"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
You can bind Product to Tag Property of Button Like this :
<Button Content="Click To Add" Tag="{Binding}"/>
You will get original entity by :
Product product = button.Tag as Product
Firstly, I am using the MVVM design pattern. I have a DataGrid, as shown in the first code snippet below, in which the RowDetailsTemplate is set to another grid, the second code snippet below. The itemsource for the parent grid is an ObservableCollection of type Adult and the itemsource for the sub-grid is an ObservableCollection of type Child, which is a property of the each Adult object. I am trying to set the SelectedItem and button command for each grid so it hits the ViewModel associated with the view and I cannot see where I am going wrong here. Any advice would be great.
<DataGrid
AutoGenerateColumns="False"
ItemsSource="{Binding AllAdults}"
SelectedItem="{Binding SelectedAdult}"
RowDetailsTemplate="{DynamicResource RowDetailsTemplate}"
RowDetailsVisibilityMode="Collapsed">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding Path=DataContext.ShowAdultInfoCommand, RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" Content="Print"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Path=AdultId, Mode=OneWay}" Header="Id" IsReadOnly="True" />
</DataGrid.Columns>
<DataTemplate x:Key="RowDetailsTemplate">
<DataGrid
ItemsSource="{Binding AllChildren}"
AutoGenerateColumns="False"
SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}, AncestorLevel=2}, Path=DataContext.SelectedChild}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding Path=DataContext.ShowChildInfoCommand, RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type DataGrid}, AncestorLevel=2}}" Content="Show"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Path=ChildId, Mode=OneWay}" Header="Child Id" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
people! I have an issue:
i have an app on MVVM Light with DataGrid:
<DataGrid ItemsSource="{Binding Path=MyCollection, Mode=TwoWay}" AutoGenerateColumns="False" CanUserAddRows="False"
FontSize="14" Name="ItemGrid" SelectionUnit="FullRow" ColumnHeaderStyle="{StaticResource ResourceKey=DataGridColumnHeader}" Background="Transparent"
Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},UpdateSourceTrigger=PropertyChanged}" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="4"
CanUserResizeRows="False" SelectionMode="Single" BorderThickness="0">
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid
IsReadOnly="True"
AutoGenerateColumns="False"
ItemsSource="{Binding MyItems}"
SelectionUnit="FullRow"
Visibility="{Binding Path=DataContext.RowDetailsVisibility, Mode=TwoWay,Converter={StaticResource BoolToVisibilityConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
CanUserResizeRows="False"
CanUserResizeColumns="True"
SelectedItem="{Binding Path=DataContext.SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Visibility="Collapsed" Binding="{Binding ID}" Width="Auto" CellStyle="{StaticResource ResourceKey=DataGridCell}" />
<DataGridTextColumn Header="Title" Visibility="Visible" Binding="{Binding Title}" Width="Auto" CellStyle="{StaticResource ResourceKey=DataGridCell}" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Item Pack ID" Width="*" CellStyle="{StaticResource ResourceKey=DataGridCell}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Id, Mode=TwoWay}" Style="{StaticResource ResourceKey=TextBoxStyle}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Number" Width="*" CellStyle="{StaticResource ResourceKey=DataGridCell}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Number, Mode=TwoWay}" Style="{StaticResource ResourceKey=TextBoxStyle}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid.Columns>
<DataGrid.ContextMenu>
<ContextMenu Name="contextMenu" Style="{StaticResource ContextStyle}">
</MenuItem>
<MenuItem Header="Packs Columns"
ItemsSource="{Binding PacksCollection}"
DisplayMemberPath="Key"
Command="{Binding Path=DataContext.PacksCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
>
</MenuItem>
<MenuItem Header="Items Columns"
Command="{Binding Path=DataContext.ItemsCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
ItemsSource="{Binding ItemsCollection}" DisplayMemberPath="Key">
</MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
</Grid>
So, if you understand, i have a grid with Packs(Id,Number),each Pack have some Items(Id,Title). I want to have a context menu, which have 2 lists: Packs and Items. In each menu item we can choose what column to show.
ItemsCollection and PacksCollection are Dictionary where String - Column Header(same as in data grid) and bool - visibility state(true for Visible, false for Collapsed).
Question: how to bind MVVM click on menu item, that will change column visibility. I tried some solutions, but they don't worked for me.
I need to change visibility for DataGrid columns(Id,Number) and for RowDetails DataGrid - Id, Title. Help please, i don't think this is so hard.
Thank you.
I need to create a Custom dataGrid DataGridTextColumn like the sketch below:
The Red rectangles are TextBox and are used to search within the column.
so far i have implemented a datagrid like this (simplify Version):
<DataGrid x:Name="CompassLogDataGrid"
Grid.Row="1"
Style="{DynamicResource ResourceKey=DataGridStyle}"
IsTextSearchEnabled="True">
<DataGrid.Columns>
<DataGridTextColumn CellStyle="{StaticResource IdCell}"
x:Name="ID"
Header="ID"
Foreground="Black"
Binding="{Binding ID}"
DisplayIndex="0" />
<DataGridTextColumn x:Name="DateGTC"
Header="Date"
Binding="{Binding DateString}"
CellStyle="{StaticResource DateGTCCell}" />
</DataGrid.Columns
</DataGrid
I have no idea how to create those textBoxes. Any clue would be appreciate it
DataGridTemplateColumn is what you are looking for. You can customize the template as per your need -
<DataGrid>
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox BorderBrush="Red" BorderThickness="3" Margin="5"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
With sample ItemsSource it gives this look -
EDIT
In case you want to customize the header, you need to provide HeaderTemplate for your column like this -
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}"
Header="{Binding HeaderName}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Content, RelativeSource=
{RelativeSource Mode=TemplatedParent}}"
Margin="5"/>
<TextBox BorderBrush="Red" BorderThickness="3"
Width="50" Margin="5"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Here's the look -