Attach behavior to Items in GridView - wpf

I've got a ListView with a GridView used to present Items. ListView is defined like
<ListView ItemsSource={Binding ...}>
<ListView.View>
<GridView>
<GridViewColumn>...</GridViewColumn>
<GridViewColumn>...</GridViewColumn>
<GridViewColumn>...</GridViewColumn>
...
</GridView>
</ListView.View>
</ListView>
How can I attach custom behavior to each Item in the List (one behavior for all items)?
Thanks in advance!

Related

WPF: set GridView ColumnHeaderContainerStyle code behind

So i have my ListView and GridView inside ListView.View:
<GridView ColumnHeaderContainerStyle="{StaticResource ListViewHeaderDefaultStyle}" >
And i Added several Styles that i am want to change via code behind.
but all found is how to change ItemContainerStyle
Just give your GridView an x:Name in your XAML:
<ListView>
<ListView.View>
<GridView x:Name="gv">
...
</GridView>
</ListView.View>
</ListView>
...and you will be able to access it in the code-behind as usual:
gv.ColumnHeaderContainerStyle = this.FindResource("ListViewHeaderDefaultStyle") as Style;

Get GridViewColumn from control inside DataTemplate

Example:
<ListView Name="lvAnyList" ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="xx" DisplayMemberBinding="{Binding XX}" CellTemplate="{DynamicResource MyDataTemplate}"/>
<GridViewColumn Header="yy">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding YY}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
If I access the TextBlock inside DataTemplate I will have access to information about the bound path.
But I don't know how to find (starting from the TextBlock) its containing GridViewColumn (which is in the GridViewRowPresenter columns list) and compare it to the grid's GridViewColumn (from GridViewHeaderRowPresenter column list) to get the header's name.
At the end I should have the pairs: xx->XX, yy->YY
I can find a list of all TextBlocks inside ListView by using this:
GridViewRowPresenter gvrp = ControlAux.GetVisualDescendants<GridViewRowPresenter>(element).FirstOrDefault();
IEnumerable<TextBlock> entb = GetVisualDescendants<TextBlock>(gvrp);
I can find a list of all GridViewColumnHeaders:
GridViewHeaderRowPresenter gvhrp = ControlAux.GetVisualDescendants<GridViewHeaderRowPresenter>(element).FirstOrDefault();
And I cannot find the connection between the TextBlocks and the GridViewColumns...
I hope this is understandable.
GridViewColumns are abstract objects they never appear anywhere in the UI, you can try to name the column and bind it to the Tag of a control where you need it using ElementName or if that fails a Source with x:Reference on the name.

How to get selected item of a nested listview?

i have a nested listview, i can bind the selected item of the basic listview to my viewmodel but not my selected item of the nested listview ( in the basic listview ) I just do:
this is my listview:
<ListView Height="155" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible" dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="False" Margin="24,506,280,169" Background="#CDC5CBC5"
dd:DragDrop.DropHandler="{Binding}" SelectedItem ="{Binding Path=SelectedCluster,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" ItemsSource="{Binding Path=Clusters,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" >
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Titel" DisplayMemberBinding="{Binding Title}"/>
<GridViewColumn Header="Questions">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ListView DataContext="{Binding}" ItemsSource="{Binding ExaminationQuestions}" SelectedItem="{Binding Path=SelectedExaminationQuestionInCluster,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}">
<ListView.View>
<GridView>
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding Question.Description}"/>
</GridView>
</ListView.View>
</ListView>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
Viewmodel:
public ExaminationQuestion SelectedExaminationQuestionInCluster
{
get { return selectedExaminationQuestionInCluster; }
set { selectedExaminationQuestionInCluster = value;
OnPropertyChanged("SelectedExaminationQuestionInCluster");
}
}
Someone who knows what i am doing wrong? If i set a breakpoint of the setter of selecteditem of the second listview. He just ignores that..
Thanks
My guess is the binding is probably incorrect. In your outer ListView, you bind to "Clusters". Your inner ListView is probably trying to bind to "SelectedExaminationQuestionInCluster" on the current Cluster. You can see if this is the case by using snoop. It's a valuable tool when debugging WPF apps. It will highlight broken bindings in red and tell you what's wrong with them.
If you want to bind to "SelectedExaminationQuestionInCluster" on the parent DataContext, you could use this syntax:
SelectedItem="{Binding Path=DataContext.SelectedExaminationQuestionInCluster,
ElementName=OuterListView}"
You'll have to give the outer ListView a name of course.
EDIT: I just realized this might not make sense. If each Cluster has its own collection of ExaminationQuestions, then each Cluster should also have a SelectedExaminationQuestion. The parent DataContext should not have any concept of a SelectedQuestion unless it is shared amongst all Clusters.

How to bind to ColumnCollection for a GridView using XAML

I have a collection of objects which I would like to represent columns in a GridView.
Currently I am binding individual columns in my GridView to items in the collection like this:
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="{Binding Path=MyColumns[0].Title}"></GridViewColumn>
<GridViewColumn Header="{Binding Path=MyColumns[1].Title}"></GridViewColumn>
<GridViewColumn Header="{Binding Path=MyColumns[2].Title}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
This works well, but what I really want to do is to bind to the collection itself (since the number of items in the collection might change at run-time). I would like to accomplish this in XAML. What I have in mind is something like this (note this does not work - the ColumnsSource attribute is make-believe):
<ListView>
<ListView.View>
<GridView ColumnsSource="{Binding Path=MyColumns}">
</GridView>
</ListView.View>
</ListView>
Can this be done? If so, how?
You can use an attached property to create a ColumnsSource property, see this question for an example.

Multicolumn listview in WPF at design time

I might sound dumb here, but I want to do something really simple. At design time, I want to add columns to a listview control and add some data to it. I need to add combobox in each column of the listview. The thing I am not able to find is where to mention the column number in the listviewitem. Any help appreciated guys.
<ListView IsSynchronizedWithCurrentItem="True" Margin="8,68,304,188"
BorderThickness="2,2,2,2">
<ListView.View>
<GridView>
<GridViewColumn Width="150" Header="Column1"/>
<GridViewColumn Width="150" Header="Column2"/>
</GridView>
</ListView.View>
<ListViewItem>
</ListViewItem>
</ListView>
Each column in the listviewitem is rendered based on the GridView definition, so there is no real concept of column numbers. What you do is bind objects to the the listview's itemsource and it creates listviewitems from it. Thus, there are a few hoops to jump through.
This link has an example of how to do some simple object data binding. The advantage of this is what binding structure you have for design time can probably be reused for run-time if you set the datacontext/itemsource to an empty object instead of the static one in XAML.
If you're doing this to show examples or you just have a static data source that you want to use, I would recommend using the XmlDataProvider. Then you'd change your ListView to be like this,
<ListView IsSynchronizedWithCurrentItem="True" Margin="8,68,304,188"
BorderThickness="2,2,2,2">
<ListView.View>
<GridView>
<GridViewColumn Width="150" Header="Column1" DisplayMemberPath="{Binding XPath=/A/B}"/>
<GridViewColumn Width="150" Header="Column2" DisplayMemberPath="{Binding XPath=/A/C"/>
</GridView>
</ListView.View>
<ListViewItem>
</ListViewItem>
</ListView>

Resources