I want to create DataGrid in which each row has column which allows user to select multiple hobbies(as shown in image).I dont know how to do this in wpf .I am new to wpf.Can anybody please help?
also i want to show one of the selected checkbox value in column.
1- Install: Install-Package Extended.Wpf.Toolkit -Version 3.4.0 (Or use NuGet in your project References).
2- Add it as Custom DataGridColumns:
<Window x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<!-- DO NOT FORGET TO ADD THIS -->
xmlns:xwt="http://schemas.xceed.com/wpf/xaml/toolkit"/>
<DataGrid>
<DataGrid.Columns>
<!-- Column 1 -->
<DataGridTemplateColumn Header="Column 1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<xwt:CheckComboBox />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Column 2 -->
<DataGridTemplateColumn Header="Column 2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<xwt:CheckComboBox />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Window>
See the CheckComboBox
Related
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>
WPF Application
In datagrid,if I use DataGridComboBoxColumn to show Combobox,it is only show when I double click the cell enter edit status.
I want to show the combobox all the time, and don't need to double click the cell.
How to do?
Replace the DataGridComboBoxColumn with a DataGridTemplateColumn and add a ComboBox to its CellTemplate and CellEditingTemplate:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="..." />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="..." />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
※ 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.
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.
So I have a custom control:
<Grid>
<StackPanel HorizontalAlignment="Center" Orientation="Vertical">
<CheckBox x:Name="chkboxListen" HorizontalAlignment="Center" Checked="chkboxListen_Checked" Unchecked="chkboxListen_Unchecked"/>
<MediaElement x:Name="mediaElementAudioPlayer" Volume="{Binding ElementName=sliderVol, Path=Value}" />
</StackPanel>
</Grid>
And it will reside within a DataGrid Template Column:
<DataGridTemplateColumn x:Name="callListenL" Header="Listen(L)" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<localControls:AudioPlay x:Name="audioPlayL" localControls:AudioPlay>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
My question is:
When I Check the chkboxListen CheckBox, how I can get the Information from the row of the DataGrid (Parent)? Each row has a myObject with an Id. I just need that Id.
Thank you in advance.
You can expose a property from within your user control and set the value of that property when the datagrid data binds.
For example, say your user control's property is called ParentRowId you could set it in code behind or use an Eval expression
<DataGridTemplateColumn x:Name="callListenL" Header="Listen(L)" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<localControls:AudioPlay x:Name="audioPlayL" ParentRowId='<%# Cint(Eval("Id")) %>' localControls:AudioPlay>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>