Binding combobox in DataTemplate to a different ItemSource - c#

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.

Related

How to access checkboxes in a List view one by one

I want to check all text boxes on the basis of if the check box is checked. But I don't know how to access all check boxes one by one? I bound it with ApprovalStatus which is of boolean type. Can any one help me to have code in C#?
<CheckBox Content="Check All" Height="16" HorizontalAlignment="Left" Margin="9,193,0,0" Name="Tab2CheckAll" VerticalAlignment="Top" Width="77" Click="Tab2CheckAll_Click"/>
<ListView Height="213" HorizontalAlignment="Left" Margin="9,215,0,0" Name="Tab2EmployeeEffortList" VerticalAlignment="Top" Width="771" AllowDrop="True" IsTextSearchEnabled="True">
<ListView.View>
<GridView>
<GridViewColumn Header="Approved" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox CommandParameter="{Binding}" IsChecked="{Binding ApprovalStatus}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
You need not to access individual checkBoxes. CheckBox is already bounded to property set that instead.
Loop over ItemsSource of ListView and set ApprovalStatus to true for all items in the collection. As long as your underlying source class is implementing INotifyPropertyChanged, it will work fine.

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.

How to bind from DataTemplate to the GridViewColumns item

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.

getting the Mouseover Event on listview in wpf

I had created the listview in below manner
using grid view init;
i need that when mouseover on the gridview item i need to get that username details in the popup window , i am able to generate the popupwindow
except that Hover username in the listview
this is my sample code so please try to provide this information
<ListView AlternationCount="2" removed="#FFB6CCD8" Canvas.Left="15" Canvas.Top="77" FontSize="13" Height="43" IsSynchronizedWithCurrentItem="True" ItemContainerStyle="{DynamicResource ListViewItemContainerStyle}" ItemsSource="{Binding}" Name="checkedListView1" SelectionMode="Single" TabIndex="2" Width="269" ItemTemplate="{StaticResource ItemDataTemplate}"
SelectionChanged="checkedListView_SelectionChanged" MouseEnter="checkedListView1_MouseEnter" MouseMove="checkedListView1_MouseMove">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=empid}" Header="CardID" Width="1" />
<GridViewColumn DisplayMemberBinding="{Binding Path=username}" Width="200" />
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="/WPF.Themes.Demo;component/Images/Status-user-online.ico" Width="20" Height="20"></Image>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
here i am using the listview in the Gridview manner , so please try to provide the Information to get the Username which was presented in the Gridviewcolumn when mouse cursor Hover in the listview
Thank you.

Having trouble binding list/grid to collection

I have set up a grid and bound it to a collection. I can edit the items in the collection through my grid and the changes get propagated to the collection. And, the GUI is showing everything in the collection at the time the ItemSource is set. But, I am programmatically changing some of the items in the collection (after the ItemSource is set) and these changes aren't reflected in the grid/GUI. Is there something else I need to do in order to get it to refresh. FYI, for the fields I want to edit (MoveToResource, ResourceKey, and Resource Type), I have set the mode to TwoWay. Below is my grid.
<ListView Name="lstXAMLStrings" Margin="5" Grid.Row="1">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Extract?">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Content="" IsChecked="{Binding Path=MoveToResource, Mode=TwoWay}" ></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Text">
<GridViewColumn.CellTemplate>
<DataTemplate>
<local:RichTextBlock RichText="{Binding Path=FormattedMatchedLines}" TextWrapping="Wrap" Width="650"></local:RichTextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Key Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=ResourceKey, Mode=TwoWay}" Width="150"></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Resource Type">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource odp}}" SelectedItem="{Binding Path=Resource, Mode=TwoWay}"></ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
Does your [view]model class implement INotifyPropertyChanged and fire the event whenever the property set accessor is used?
You need to make sure that the collection itself that you're databinding to is an observable collection (a class that implements the INotifyCollectionChanged interface). You might be able to alternatively roll your own class that implements INotifyCollectionChanged, but that's the only reason ObservableCollection exists so it could save you some time.
There's an msdn article on how to do it.
You need to make sure that your collection items implement INotifyPropertyChanged.
If each item you're changing programatically (correctly) implements that, your ListView/GridView will stay current.
This will work if you modify your collection items programatically, or in another screen.

Categories