DataGridTemplateColumn binding converter - wpf

I am using the following code :
Where is my mistake? What's the solution?
<MyClass:CheckForVis x:Key="_CheckForVis"/>
</Window.Resources>
<DataGrid Name="dg" AutoGenerateColumns="False"
CanUserAddRows="False" CanUserDeleteRows="False" CanUserSortColumns="False"
ItemsSource="{Binding ElementName=tempWin, Path=TitleClass}" >
<DataGrid.Columns>
<DataGridTemplateColumn Visibility="{Binding ElementName=tempWin, Path=TitleClass,Converter={StaticResource _CheckForVis}}" Width="*" Header="Input" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="10,0,0,0" Grid.Column="0" Name="partMojudi">
<TextBlock Width="Auto" Name="txtInput" Text="{Binding input}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>
</Grid>
</Window>
Why not right?
Where is my mistake? What's the solution?

Related

WPF DataGrid Master/Detail: How to show Master property within Detail that's within a different ItemSource

I have an object hierarchy of this: Orders have Allocations. My DataGrid has an ItemSource of Orders which then shows some details of each order in each grid row. Upon clicking on a grid row, a details pane comes up with additional details including a list of Allocations for each order. What I need to do is to somehow reference back to the Master record and pull a bit of detail (which would end up repeating) for each allocation. If I try to RelativeSource back to the DataGrid, I cannot refer to the property since that source is a whole collection and I need to get the value off of the item of that collection.
Below is a simplified version of the XAML I'm using. Basically, I need to bind to QuantityTypeDescription that's in the master record from each Allocation item in the child (Order) record.
<DataGrid ItemsSource="{Binding Orders}" AutoGenerateColumns="False" IsReadOnly="True" HorizontalScrollBarVisibility="Auto">
<DataGrid.Columns>
<DataGridTextColumn Header="Trade" Binding="{Binding TransactionTypeDescription}" Width="*"/>
<DataGridTextColumn Header="Adjustment" Binding="{Binding QuantityTypeDescription}" Width="Auto"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<!--Order Summary-->
<StackPanel Orientation="Vertical">
<StackPanel Background="DarkSlateGray" Orientation="Vertical">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock Text="{Binding TransactionTypeDescription}" FontWeight="Bold" Margin="5,0,0,0" FontSize="14" Foreground="White"/>
</StackPanel>
<TextBlock Name="Name" Text="{Binding SecurityName}" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White" Background="DarkSlateGray"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<ItemsControl ItemsSource="{Binding Allocations}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
NOTE: Here I need to get back up one level to get QuantityTypeDescription
<!--<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid, AncestorLevel=1}, Path=QuantityTypeDescription}" Margin="5,0,0,0"/>-->
<TextBlock Text="{Binding Amount}" Margin="5,0,0,0"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
Use ElementName Binding to refer to your main DataGrid's SelectedItem
<DataGrid Name="MainDataGrid" ItemsSource="{Binding Orders}" AutoGenerateColumns="False" IsReadOnly="True" HorizontalScrollBarVisibility="Auto">
<DataGrid.Columns>
<DataGridTextColumn Header="Trade" Binding="{Binding TransactionTypeDescription}" Width="*"/>
<DataGridTextColumn Header="Adjustment" Binding="{Binding QuantityTypeDescription}" Width="Auto"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<!--Order Summary-->
<StackPanel Orientation="Vertical">
<StackPanel Background="DarkSlateGray" Orientation="Vertical">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock Text="{Binding TransactionTypeDescription}" FontWeight="Bold" Margin="5,0,0,0" FontSize="14" Foreground="White"/>
</StackPanel>
<TextBlock Name="Name" Text="{Binding SecurityName}" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White" Background="DarkSlateGray"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<ItemsControl ItemsSource="{Binding Allocations}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ElementName=MainDataGrid,Path=SelectedItem.QuantityTypeDescription}" Margin="5,0,0,0"/>
<TextBlock Text="{Binding Amount}" Margin="5,0,0,0"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>

Add Row to DataGrid if ItemSource Binding returns no data

is there any simple solution to this problem..first i thought i can create an empty ObservableCollection and writte:
ObservableCollection<PersonDetailsView> newOCollection= new ObservableCollection<PersonDetailsView>();
myDataGrid.ItemsSource=newOCollection.ToArray();
and it works..i get an empty row..but the problem is that i loose the other binding..is there any way to Bind to newOCollection only if my {Binding Person} (see the code) doesn't return a value
<DataGrid CanUserAddRows="True" IsReadOnly="False" BorderBrush="#FFCCCCCC"
GridLinesVisibility="All" AutoGenerateColumns="False"
ItemsSource="{Binding Person}" Background="White" Margin="10,45,0,0"
VerticalAlignment="Top" Height="91" HorizontalAlignment="Left"
HeadersVisibility="None" SelectionMode="Single" Name="dtaPersons" Width="415">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Person.Name, Mode=TwoWay}" Width="Auto"/>
<DataGridTextColumn Binding="{Binding Person.Surname, Mode=TwoWay}" Width="*"/>
<DataGridTemplateColumn Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<Button Style="{DynamicResource ChromelessButtonStyle}" Content="r" FontFamily="Marlett" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
ObservableCollection<PersonDetailsView> newOCollection= new ObservableCollection<PersonDetailsView>();
if (!myDataGrid.Items.Any())
myDataGrid.ItemsSource=newOCollection.ToArray();

WPF databinding a gridview issue

I am working on a dataGrid that is populated from an Entity model. I have the following for the specific column as an attempt to use a combobox during editing and the source for the editing is a CollectionsViewSource.
<Grid DataContext="{StaticResource vsLogins}" Grid.Column="1"
Margin="16,248,154,31">
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True"
Height="213" HorizontalAlignment="Left"
ItemsSource="{Binding Source={StaticResource vsLogins}}"
Name="LoginsDataGrid"
RowDetailsVisibilityMode="VisibleWhenSelected"
VerticalAlignment="Top" Width="380"
Background="{StaticResource lgb}" BorderThickness="2"
BorderBrush="#FFFC0303">
<DataGrid.Columns>
<DataGridTextColumn x:Name="LoginNameColumn" Binding="{Binding
Path=LoginName}" Header="Login Name"
Width="200" MinWidth="200" />
<DataGridTextColumn x:Name="PsWrdColumn"
Binding="{Binding Path=PsWrd}"
Header="Password" Width="130" MinWidth="130" />
<DataGridTemplateColumn x:Name="AccessLevelIdColumn" Header="Id"
Width="40" MinWidth="40">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=AccessLevelId}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource
vsAccessLevels}}" DisplayMemberPath="Description"
SelectedValuePath="AccessLevelId" IsEditable="False"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
It is throwing some errors. What am I doing wrong? I also tried putting a grid around the combobox and setting it's dataContent to the CVS - no dice either. Thanks for any help.
<DataGridComboBoxColumn x:Name="AccessIdColumn"
ItemsSource="{Binding Source={StaticResource vsAccessLevels}}"
DisplayMemberPath="Description"
SelectedValuePath="AccessLevelId"
SelectedValueBinding="{Binding Path=AccessLevelId}"/>

WPF 3.5 nested datagrid row details not showing in Visual Tree

So I build a nice nested datagrid that look something like this:
<dg:DataGrid x:Name="mainGrid"
AutoGenerateColumns="False"
CanUserAddRows="False"
AreRowDetailsFrozen="True"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserResizeRows="False"
SelectionUnit="FullRow"
RowDetailsVisibilityMode="VisibleWhenSelected">
<dg:DataGrid.Columns>
<dg:DataGridTemplateColumn CanUserResize="False" CanUserSort="False" Width="36" IsReadOnly="False">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="+" Width="28" Height="28" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"/>
</Grid>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn Header="Name" IsReadOnly="True" Width="582">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="5" VerticalAlignment="Center" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=Default}" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn Header="Status" IsReadOnly="True" Width="150">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="5" VerticalAlignment="Center" Text="{Binding Status, Mode=TwoWay, UpdateSourceTrigger=Default}" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridCheckBoxColumn Binding="{Binding IsSelected}"/>
</dg:DataGrid.Columns>
<dg:DataGrid.RowDetailsTemplate>
<DataTemplate>
<dg:DataGrid x:Name="scondaryDatagrid" Margin="29,0,0,0"
AutoGenerateColumns="False"
CanUserAddRows="False"
AreRowDetailsFrozen="True"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserResizeRows="False"
SelectionUnit="Cell"
RowDetailsVisibilityMode="VisibleWhenSelected"
ItemsSource="{Binding Grades, Mode=TwoWay}">
<dg:DataGrid.Columns>
<dg:DataGridTemplateColumn CanUserResize="False" CanUserSort="False" Width="32" IsReadOnly="True">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="+" Width="28" Height="28" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn CanUserResize="False" CanUserSort="False" Width="32" IsReadOnly="True">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" VerticalAlignment="Center" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn Header="Name" IsReadOnly="True" Width="550">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="5" Text="{Binding Name, Mode=TwoWay}" VerticalAlignment="Center" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
</dg:DataGrid.Columns>
<dg:DataGrid.RowDetailsTemplate>
<DataTemplate>
<dg:DataGrid Margin="25,0,0,0"
AutoGenerateColumns="False"
CanUserAddRows="False"
AreRowDetailsFrozen="True"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserResizeRows="False"
SelectionUnit="Cell"
ItemsSource="{Binding Notes, Mode=TwoWay}">
<dg:DataGrid.Columns>
<dg:DataGridTemplateColumn CanUserResize="False" CanUserSort="False" Width="32" IsReadOnly="True">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" VerticalAlignment="Center" HorizontalAlignment="Center" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn Header="Note" IsReadOnly="True" Width="550">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="5" Text="{Binding Note, Mode=TwoWay}" VerticalAlignment="Center" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>
</DataTemplate>
</dg:DataGrid.RowDetailsTemplate>
</dg:DataGrid>
</DataTemplate>
</dg:DataGrid.RowDetailsTemplate>
</dg:DataGrid>
Due to the RowDetailsVisibilityMode property when it is set to VisibleWhenSelected, the Node DataGridDetailsPresenter for the rows have 0 children. When the property is set to Visible I can see the DataGridDetailsPresenter children.
How can I force the inclusion of all the nodes in the Visual tree for when the RowDetailsVisibilityMode property is VisibleWhenSelected?
Any input is greatly appreciated.
What are you trying to accomplish?
Unless the the RowDetails are visible they would not be in the Visual Tree unless could force their creation by setting the RowDetailsVisibilityMode to Visible but setting the RowDetails DataGrid visibility to Collapsed. This may not work. I don't know if the DataGrid would be in the Visual Tree if set to Collapsed in 3.5.

Why this code doesn't work? :(

<dg:DataGrid Name="gridList" AutoGenerateColumns="False"
ItemsSource="{Binding ItemsInTable}" HeadersVisibility="Column" >
<dg:DataGrid.RowDetailsTemplate >
<DataTemplate x:Name="ItemDetails" >
<Border HorizontalAlignment="Stretch" CornerRadius="5" Background="Azure" >
<StackPanel Orientation="Vertical" Margin="0,10,0,0">
<ComboBox Name="cboxDepartment" SelectedItem="{Binding Department}" DisplayMemberPath="Desc" ItemsSource="{Binding DeptosInTable}" ></ComboBox>
</StacPanel>
</Border>
</DataTemplate>
</dg:DataGrid.RowDetailsTemplate>
<dg:DataGridTextColumn Header="DEPARTMENT" Binding="{Binding Department}" Width="180" IsReadOnly="True">
Department is a property in a class. DeptosInTable is a ObservableCollection with 2 properties: ID and Desc.
One error you have made (I'm not sure if it's the only one):
</StacPanel>
Should be:
</StackPanel>

Resources