Why this code doesn't work? :( - wpf

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

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>

wpf ListBox looks like datagrid?

I have a listbox control in my app.
I want to change his style to being look like to Datagrid (borders, columns, row...).
I don't want to using stantard datagrid - because its control cannot binding itemtemplte.
I trying to do it:
<ListBox
ItemsSource="{Binding Items}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Name="listBox1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Border BorderThickness="1" BorderBrush="Black">
<TextBlock Text="{Binding Id}" Margin="5"/>
</Border>
<Border BorderThickness="1" BorderBrush="Black">
<TextBlock Text="{Binding Name}" Margin="5"/>
</Border>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But it not looking good - as following:
it what i want to achieve:
Using Grid.IsSharedSizeScope
result
i believe you want the columns to be re-sized based on your string length, so Grid.IsSharedSizeScope is your choice here
example xaml
<ListBox ItemsSource="{Binding Items}"
Grid.IsSharedSizeScope="True">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="name" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border BorderThickness="1"
BorderBrush="Black">
<TextBlock Text="{Binding Name}"
Margin="5" />
</Border>
<Border BorderThickness="1"
Grid.Column="1"
BorderBrush="Black">
<TextBlock Text="{Binding Id}"
Margin="5" />
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
if you move SharedSizeGroup to id like below
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition SharedSizeGroup="id" />
</Grid.ColumnDefinitions>
result
Using ListView with GridView
You have an option to use list view with grid view which will have same appearance as grid with flexibility of list
eg
<ListView ItemsSource="{Binding SourceItems}">
<ListView.View>
<GridView>
<GridViewColumn Header="Column1"
DisplayMemberBinding="{Binding Column1}" />
<GridViewColumn Header="Column2"
DisplayMemberBinding="{Binding Column2}" />
</GridView>
</ListView.View>
</ListView>
GridViewColumn offers you to modify CellTemplate, HeaderTemplate, HeaderContainerStyle, HeaderStringFormat etc.
I am sure you can achieve this using a grid control (It supports binding and everything else)
To fix your problem, you will have to give fixed widths to both your borders inside your stackpanel then your listbox items will look like a grid control.
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Border Width="150" BorderThickness="1" BorderBrush="Black">
<TextBlock Text="{Binding Id}" Margin="5"/>
</Border>
<Border Width="50" BorderThickness="1" BorderBrush="Black">
<TextBlock Text="{Binding Name}" Margin="5"/>
</Border>
</StackPanel>
Please let us know what problem you are having with GridControl and maybe we can fix that as well
Edit. If you were using a DataGrid your template would look like
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" HeadersVisibility="None">
<DataGrid.Columns>
<DataGridTemplateColumn Header="" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text={Binding Id}/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text={Binding Name}/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Please note that I have set HeaderVisibility to false so that it doesnt look like a datagrid but instead looks like a list

DataGridTemplateColumn binding converter

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?

Datagrid Column Header Templating

I want to template some headercolumns in a pretty simple way, but I don't get the results I am looking for. I want the Border to fill the whole header, not just the textbox inside. Right now I am still having the default gray behind my own controls.
<DataGrid x:Name="grdItems"
Grid.Row="0"
CanUserAddRows="False"
CanUserDeleteRows="False"
ItemsSource="{Binding Path=Items}"
CanUserSortColumns="False" AutoGenerateColumns="False" CanUserResizeColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource GetSignalTypeValues}}" Width="auto" SelectedValueBinding="{Binding SignalType}" >
<DataGridComboBoxColumn.HeaderTemplate>
<DataTemplate>
<Border Background="Orange" >
<TextBlock Text="Signal Type" />
</Border>
</DataTemplate>
</DataGridComboBoxColumn.HeaderTemplate>
</DataGridComboBoxColumn>
<DataGridTextColumn Width="auto" Binding="{Binding AmplitudeMax}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<Border Background="Violet">
<StackPanel>
<TextBlock Text="Amplitude" HorizontalAlignment="Center" />
<TextBlock Text="-maximum-" HorizontalAlignment="Center" />
</StackPanel>
</Border>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
If you are only after background color of column header you can utilize this:
<DataGridComboBoxColumn Width="auto" SelectedValueBinding="{Binding SignalType}" >
<DataGridComboBoxColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="Orange"/>
</Style>
</DataGridComboBoxColumn.HeaderStyle>

silverlight printing using davidpoll.com collection printer

I am working in a Silverlight 4 application. I have a datagrid that I would like to print. I am trying to use davidpoll printing functionality
Currently, I am printing just the datagrid header, but no items are being printed
<SLaB:CollectionPrinter x:Name="printer" ItemsSource="{Binding CurrentSearchView}" >
<SLaB:CollectionPrinter.BodyTemplate>
<DataTemplate>
<sdk:DataGrid x:Name="dgResults" VerticalAlignment="Stretch" SelectionMode="Single" HorizontalAlignment="Stretch" AutoGenerateColumns="False" ItemsSource="{Binding CurrentSearchView}" IsReadOnly="True" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled" Margin="10" FontSize="12" FontFamily="Segoe UI" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding PeopleId}" Header="People ID" Width="150"/>
<sdk:DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="*" MinWidth="80"/>
<sdk:DataGridTemplateColumn Header="FQ" Width="80">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel x:Name="spFq" Background="{Binding FqBackground}" Margin="2">
<Image Source="{Binding FqImagePath}" Margin="4" Height="24" Width="24"/>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="LQ" Width="80" CellStyle="{StaticResource BrowseQualityCellStyle}">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel x:Name="spLq" Orientation="Horizontal" Background="{Binding LqBackground}" Margin="2" >
<myControls:PlayButtonControl x:Name="playControl" Margin="4" Height="24" Width="24" />
<Image Source="{Binding LqImagePath}" Margin="4" Height="16" Width="16"/>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn Binding="{Binding Format}" Header="Format" Width="Auto"/>
<sdk:DataGridTextColumn Binding="{Binding Duration}" Header="Duration" Width="Auto"/>
<sdk:DataGridTextColumn Binding="{Binding Attachment}" Header="Attachment" Width="Auto"/>
<sdk:DataGridTextColumn Binding="{Binding Restore}" Header="Restore" Width="Auto"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</DataTemplate>
</SLaB:CollectionPrinter.BodyTemplate>
</SLaB:CollectionPrinter>
And here is the button command
<Button Command="{Binding PrintCommand, ElementName=printer}">Print</Button>
Note that both, the collectionprinter and datagrid are referencing the same property CurrentSearchView, which is a PagedCollectionView. I think that my problem lies there, maybe I am not able to used this kind of collection (i also try with an observable collection and found the same results), or my bindings are wrong (having both controls referencing the same property, doesn't seem right)
Any help will be greatly appreciated.
Thanks
I have found the problem. I needed to set the binding of the datagrid to "CurrentItems"
<sdk:DataGrid x:Name="dgResults" VerticalAlignment="Stretch" SelectionMode="Single" HorizontalAlignment="Stretch" AutoGenerateColumns="False" ItemsSource="{Binding CurrentItems}" IsReadOnly="True" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled" Margin="10" FontSize="12" FontFamily="Segoe UI" >

Resources