How to bind from DataTemplate to the GridViewColumns item - c#

I have a ListView with DataTemplate defined in Resources. I would like to forward the content of the GridViewColumn to the DataTemplate so that the DataTemplate can be reused for multiple GridViewColumns. I have a property called Property1 which I want to bind to the GridViewColumn and then forwarded it to the DataTemplate where it will be displayed in the TextBlock. However, GridViewColumn doesn't have any Content property I could bind it to.
Here is a stripped down code:
<ListView>
<ListView.Resources>
<DataTemplate x:Key="PropertyTemplate>
<TextBlock Text"{Binding}" />
<DataTemplate>
</ListView.Resources>
<ListView.View>
<GridView>
<!-- No Content property in GridViewColumn -->
<GridViewColumn CellTemplate="{StaticResource PropertyTemplate}" Content={Binding Property1} />
<GridView>
</ListView.View>
</ListView>
How can I forward the the bound property from the GridViewColumn to the GridViewColumn's DataTemplate?

Update:
This should work:
<ListView ItemsSource="{Binding Items}">
<ListView.Resources>
<DataTemplate x:Key="PropertyTemplate">
<TextBlock Text="{Binding Property1}" />
</DataTemplate>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn CellTemplate="{StaticResource PropertyTemplate}" />
</GridView>
</ListView.View>
</ListView>
Previous answer (doesn't work):
Use DisplayMemberBinding property. You also probably want to set the header text for your column:
<GridView>
<GridViewColumn CellTemplate="{StaticResource PropertyTemplate}" DisplayMemberBinding={Binding Property1} Header="Some column"/>
</GridView>
You will also need to set ItemsSource on your ListView.

Related

how i access textbox inside a listview Column

i have a list view like this .
<ListView x:Name="Source_List"
ItemsSource="{Binding Lines}"
IsSynchronizedWithCurrentItem="True"
SelectionChanged="Source_List_SelectionChanged">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Line"
Width="50"
DisplayMemberBinding="{Binding LineNumber}" />
<GridViewColumn Header="Start Time"
Width="100"
DisplayMemberBinding="{Binding StartTime , Converter={StaticResource LineTimeToString}}" />
<GridViewColumn Header="End Time"
Width="100"
DisplayMemberBinding="{Binding EndTime ,Converter={StaticResource LineTimeToString}}" />
<GridViewColumn Header="Text"
Width="500">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Context ,Mode=TwoWay}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Original Text"
DisplayMemberBinding="{Binding Context ,Mode=OneTime}" />
</GridView>
</ListView.View>
</ListView>
I want to access the text box inside the selected items as a textbox.(in code behind )
how can i do this?
i used this article by beth massi.
you can acces to textbox ui element (if your texblock is clicked)
TextBlock content = ((FrameworkElement)e.OriginalSource) as TextBlock;
else if your texblock is inside a grid use
Grid c = ((FrameworkElement)e.OriginalSource) as Grid;
and search for the grid(c) children.
Because you are using IsSynchronizedWithCurrentItem="True" you should be able to access the current item of your list 'Lines'. Your text box is bound to Context so I would assume in your view model you would be able to call 'Lines.CurrentItem.Context'

showing multiple items inside listview

I was wondering if there is a way to show multiple elements inside listview.view.
SampleCode (which doesnst work)
<ListView
x:Name="Somename"
ItemsSource="{Binding someObservableCollection}">
<ListView.View>
<TextBlock></TextBlock>
<TextBlock></TextBlock>
<TextBlock></TextBlock>
<GridView>
<GridViewColumn Header="Column 1" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Column 2">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Sample Data properties for a person.
Name
Age
Id
OldData1
OldData2
OldData3
NewData1
NewData2
NewData3
I am trying to show Name, age, Id and 3 textblocks
and then in a grid, compare olddata vs new data.
I tried doing with itemtemplate and then doing stackpanel with grid inside, but that grid looks very basic and would prefer gridview since it looks better.

WPF ListView - 2 dimensions with 1 same structure

We are working on a ListView (C# WPF) and we didn't found how bind a list of items in columns, with each item containing itself a list of items with the same columns.
Let's illustre this in an example :
!
We got an observable collection on objects with parameters (name, etc.) and each object contain another observable collection of objects with the sames parameters (exept they haven't a list). So we want to list it in a ListView but we can't figure how !
We do not know enough ListView to implement this structure, some advices ?
Thanks in advance
I think the best way is :
XAML :
<Window.Resources>
<DataTemplate x:Key="gridViewSecondCellTemplate1">
<StackPanel Width="100">
<TextBlock Text="{Binding Content}" FontSize="15" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="gridViewCellTemplate1">
<StackPanel Width="100">
<TextBlock Text="{Binding Title}" FontSize="15" />
<ListView ItemsSource="{Binding MySecondSource}>
<ListView.View>
<GridView>
<GridViewColumn Header="{Binding Subtitle}" CellTemplate="{StaticResource gridViewSecondCellTemplate1}"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</DataTemplate>
</Window.Resources>
<ListView ItemsSource="{Binding MySource}">
<ListView.View>
<GridView>
<GridViewColumn Header="Col 1" Width="100" CellTemplate="{StaticResource gridViewCellTemplate1}"/>
</GridView>
</ListView.View>
</ListView>
I didn't try this code. Try it, and say me if it is OK.

Binding combobox in DataTemplate to a different ItemSource

I have 2 ObservableCollection lists, which we can call A and B, then I have a GridView that I want to bind to list A and a ComboBox INSIDE that GridView, that I want to bind to list B.
I've set the ItemsSource property of the GridView by code: gridview.ItemsSource=A (and it works!). About the ComboBox its instance it is not available by code, I suppose because its definition it is enclosed between the DataTemplate tags; so I wonder how to bind the combo to list B, either by code or by XAML.
Follows the XAML code:
<ListView Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="lstReplacements" VerticalAlignment="Stretch">
<ListView.View>
<GridView>
<GridViewColumn HeaderContainerStyle="{StaticResource MyHeaderStyle}" Header="Wrong text" DisplayMemberBinding="{Binding Word}"/>
<GridViewColumn HeaderContainerStyle="{StaticResource MyHeaderStyle}" Header="Replacement" Width="60" DisplayMemberBinding="{Binding Replacement}" />
<GridViewColumn HeaderContainerStyle="{StaticResource MyHeaderStyle}" Header="Type" Width="30">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{??????}" DisplayMemberPath="??????" Grid.Row="1" Grid.Column="0" Name="cmbCorrectionType" Width="75" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Thanks in advance for the support!
Chris
I assume this control is in UserControl and you have set DataContext of that UserControl to the class instance where your both collections CollectionA and CollectionB resides.
You can then bind using RelativeSource:
<ComboBox ItemsSource="{Binding DataContext.CollectionB,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=UserControl}}"/>
Also you can set DataContext of ListView to the class instance and all you need to do is change AncestorType to ListView in place of UserControl in above binding.

Checkbox in gridviewcolumn(header)

I am using a listview (gridview/gridviewcolumn)
where the first column
contains only checkboxes for each row. Instead of adding a select all button I want
to add a Checkbox into the header of the first column.
Selecting the checkbox in the header will select
all other checkboxes and vice versa.
How can I do that in xaml?
Update:
This is the important part of my xaml code. (simplified)
<ListView ItemSource="...">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
To have a checkbox on top of GridViewColumn you can do something like this
<GridViewColumn>
<GridViewColumn.Header>
<CheckBox/>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Now you can handle the check event and in the code you can iterate through your itemsSource and change values or if you are following MVVM you can bind property with checkbox so that whenever check is changed you will be notified through INotifyPropertyChanged. Once you find out through binding that check has changed, again you can change your itemssource accordingly

Categories