ListView split row cells in wpf - wpf

※ Add Info
I forgot to tell one thing is each list item have to get their own color (refer image)
Click Here!
I already tried to using stackpanel, but it coudln't display the color for each items.
I'm trying to find the way split the specific row in wpf.
Image
The following attachment is the format what i want to make by wpf.
To merge Row is not what I want.(becacuse I want to split the row data in one instance)
The data(1~3-3) is member property of each Instance.
Does anybody can solve this problem?
I'll look forward to you guys answer.
thank you

You can use a DataGrid instead like this:
<DataGrid Name="dg" Width="400" Height="300" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTemplateColumn Header="Addresses">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Address1}" />
<TextBlock Text="{Binding Address2}"/>
<TextBlock Text="{Binding Address3}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Please change the bindings of the columns with your data context properties.

Related

How can I create a second row of headers with TextBoxes on it on my DataGrid?

I have created a DataGrid and now want a second row of headers in the TextBoxes. I want it to look like this in the end. There are already similar examples on the site but they couldn't help me. I would be very grateful if someone could give me a Xaml code to implement it as shown in the example.
Your best bet is to add a TextBox to the header of each column:
<DataGrid ...>
<DataGrid.Columns>
<DataGridTextColumn SortMemberPath="YourProperty" Binding="{Binding YourProperty}">
<DataGridTextColumn.Header>
<StackPanel>
<TextBlock Text="header..." />
<Separator />
<TextBox/>
</StackPanel>
</DataGridTextColumn.Header>
</DataGridTextColumn>
...
</DataGrid.Columns>
</DataGrid>

Weird collapsed column headers

When setting the Width of a data grids colum to * the columns will appear somehow collapsed like in the screenshot below.
After I add some data to the data grid and somehow force a redraw, the columns suddenly appear as expected. What am I doing wrong here?
I'm using DataGridTemplateColumn. Below is some sample code:
<DataGridTemplateColumn Width="*"
Header="Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding FullName, ValidatesOnNotifyDataErrors=True}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding FullName, ValidatesOnNotifyDataErrors=True, TargetNullValue={x:Static sys:String.Empty}, Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
I found the issue: I added an empty group style and that causes the columns to appear like shown in the screenshot above.

How to restrict the key-board operation to change the check-box value?

I have loaded the check-box in the DataGrid cell of a custom column, i like to restrict the Editing operation for that column So that i have customize the Column externally and then make the Check-box property IsHitTestVisible as false to restrict the editing operation for the check-box using mouse.But i am able to change the check-box status using the key-board(Space key). How to avoid this and makes the check-box completely in the non-editable state.
Code snippet`
< CustomDataGrid x:Name="dataGrid"
Editing="True" IsReadOnly="True"
Grouping="True"
AutoPopulateColumns="False"
DataSource="{Binding Path=OrdersDetails}">
< CustomDataGrid.Columns>
//I like to restrict the editing for the Closed column, Like IsReadOnly property of the Text-Box,
I am able to achieve this using the IsHitVisible as false,This helps only for mouse click But I am not able to restrict the key opeartion
<MyDataGridCheckBoxColumn Text="Closed" Items="{Binding IsClosed}" />
< CustomDataGrid.Columns>
<CustomDataGrid/>
`
While it is unclear as to all your custom columns and the requirements. This should be able to be solved with a Template for the given Column. I Mocked up a quick example:
<DataGrid ItemsSource="{Binding Tester}" Width="280">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="100" IsReadOnly="True" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Value" Width="100" IsReadOnly="True" Binding="{Binding Value}"/>
<DataGridTemplateColumn Header="Checked" Width="80">
<!-- Should be able to put a similar Template on MyDataGridCheckBoxColumn-->
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}" IsHitTestVisible="False">
</CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
With this I am unable to change the checkbox with the Mouse and the Keyboard.

WPF DataGrid Not Sorting

I am trying to get a WPF DataGrid to sort by a column by default and it isn't working. there is no sorting happening. The sort icons appear and if i click them then sorting happens.
Right now my table has just the one column. but is it still not sorting it by default.
Any ideas what i am missing?
<DataGrid.Columns>
<!--Ordinal-->
<DataGridTemplateColumn d:DataContext="{d:DesignInstance tabViewModels:ColumnViewModel}"
SortMemberPath="Ordinal"
>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<Label Content="#" ToolTip="Column Position" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<xctk:DoubleUpDown Value="{Binding Ordinal, UpdateSourceTrigger=PropertyChanged, TargetNullValue=''}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Ordinal}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
One solution would be to use a LINQ query to give the data already sorted to the DataGrid. Modify the getter of the Item Source with something like this:
List<Something> MyItemSource
{
get
{
return _myItemSource.OrderBy(x => x.Ordinal).ToList();
}
}
then it will for sure sort by Ordinal.

How to set a specific Datacontext for a DataTemplate in CellTemplate

Currently i bind to a List<T> so i have to do specific set foreach Column a separate DataTemplate
like this:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Center"
Text="{Binding ObColl[1].Std, UpdateSourceTrigger=PropertyChanged}"
Background="{Binding ObColl[1].DienstColor, TargetNullValue=Transparent,FallbackValue=Transparent}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
but i want is to create the DataTemplate one time as Resources
<DataGrid.Resources>
<DataTemplate x:Name="MyCellTemplate">
<TextBlock TextAlignment="Center"
Text="{Binding Std, UpdateSourceTrigger=PropertyChanged}"
Background="{Binding DienstColor, TargetNullValue=Transparent,FallbackValue=Transparent}" />
</DataTemplate>
</DataGrid.Resources>
and use it like
<DataGridTemplateColumn CellTemplate="{StaticResource MyCellTemplate} ??{Binding ObColl[1]}??"/>
But to do so i need to specific the DataContext (ObColl[Idx]) in my DataGridTemplateColumn
but how do i do this?
EDIT
the xaml should look like :
<DataGrid Name="dataGrid1"
ItemsSource="{Binding Itemlist, UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Resources>
<DataTemplate x:Key="MyCellTemplate">
<TextBlock TextAlignment="Center"
Text="{Binding Std, UpdateSourceTrigger=PropertyChanged}"
Background="{Binding DienstColor, TargetNullValue=Transparent, FallbackValue=Transparent}" />
</DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<!-- Column 1 -->
<DataGridTemplateColumn CellTemplate="{StaticResource MyCellTemplate}"
DataContext={Binding ObColl[0]}/>
<!-- Column Header 2 -->
<DataGridTemplateColumn CellTemplate="{StaticResource MyCellTemplate}"
DataContext={Binding ObColl[1]}/>
</DataGrid.Columns>
</DataGrid>
the DataContext={Binding ObColl[1]} is the problem part because it doesn't exist ....
Ok, here is my understanding of you requirement... you have a MyRow class with two properties; MyRowheader and MyCellList. You want to display the MyRowheader value and one value from the MyCellList collection on each row of your DataGrid. This is how I would do that:
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding YourCollection}">
<DataGrid.Columns>
<DataGridTextColumn Header="Header" Binding="{Binding MyRowheader,
UpdateSourceTrigger=PropertyChanged}" />
<DataGridTemplateColumn Header="Cell list">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding MyCellList[1].Std, UpdateSourceTrigger=
PropertyChanged}" Background="{Binding MyCellListl[1].DienstColor, TargetNullValue=
Transparent, FallbackValue=Transparent}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Please let me know if I have misunderstood your requirement.
UPDATE >>>
So I did misunderstand your requirement. It seem as though you want one value from your MyCellList collection in each column, not row, of the DataGrid. In that case, my answer would be no, you can't setup your DataGrid.Columns using a DataTemplate or any other XAML saving feature. XAML is a verbose language... there are a few ways of writing it more efficiently, but not many. You will often find repeated code on XAML pages.
The only way that I can think of that you could write less code would be if you dynamically generated the columns from code. You can find a basic example of that in the Dynamically add Columns to DataGrid in wpf post. I don't know how much time that will save you though really.

Resources