WPF nested objects databinding - wpf

Hi I am having issues with understanding WPF databinding with nested objects.
I have a workgroup class containing a List of User_activation objects called ListMembers and I would like to display its properties. How do I access its nested properties? This class contains another object called User that has its username and ultimately I would like to display the username in the combobox instead of WPF_test.User_activation.
Below is the XAML code and corresponding layout:
<ListView x:Name="ListViewWorkgroups" VerticalAlignment="Top" Height="Auto" Width="Auto" ItemsSource="{Binding listWorkgroups}">
<ListView.View>
<GridView>
<GridViewColumn Width="auto" Header="Workgroup" DisplayMemberBinding="{Binding Name}"></GridViewColumn>
<GridViewColumn Width="auto" Header="Skills">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ListSkills}" ></ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="auto" Header="Members">
<GridViewColumn.CellTemplate>
<DataTemplate >
<ComboBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ListMembers}" ></ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Layout: http://i50.tinypic.com/ydy5h.png
Thank you!

You need to set the ItemTemplate for the ComboBox
<ComboBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ListMembers}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding User.Username}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
As an alternative, if you don't need anything complex you can bind the DisplayMemberPath
<ComboBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ListMembers}" DisplayMemberPath="{Binding User.Username}"/>
You use the "." to access properties like you would in normal c# code

This is just a follow-up to the previous answer. I just discovered that in Bindings, you may use a leading period in a filesystem fashion:
<ComboBox ItemsSource="{Binding .ListMembers}">
<DataTemplate>
<TextBlock Text="{Binding .User.Username}"/>
</DataTemplate>
That syntax adds nothing semantically, but in some cases makes the statement more readable (and XAML can certaintly use that!)
Here's a better example:
<ComboBox ItemsSource="{Binding Caliber}" DisplayMemberPath=".Thickness" />
where Thickness is a property of Caliber.

Related

Binding to elements in WPF GridViewHeader doesn't work

I'm trying to create a custom header for a ListView / GridView where the user can easily filter items using Text inputs:
I've created DependencyProperties on the UserControl's code-behind and are binding the Xaml datacontext to itself (name=root). The binding i'm having trouble with is the TextBox inside the Header - it simply doesn't work. If i set a 'ModelVariantId' to "Whatever" it doesn't show and likewize i don't get any PropertyChanged events when writing in it..
I've found -some- similar sounding questions on the internet but nothing exactly matching.
<ListView ItemsSource="{Binding ElementName=root, Path=CombinedCarModels}">
<ListView.View>
<GridView>
<GridViewColumn Width="120" DisplayMemberBinding="{Binding ModelVariantId}">
<GridViewColumn.Header>
<GridViewColumnHeader HorizontalContentAlignment="Stretch">
<StackPanel>
<Label>ModelVariantId</Label>
<TextBox Text="{Binding ElementName=root,
Path=CarModelFilter.ModelVariantId, Mode=TwoWay}" />
</StackPanel>
</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
If you define a HeaderTemplate, this binding should work:
Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl},
Path=CarModelFilter.ModelVariantId, Mode=TwoWay}"
XAML:
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Row="0" HorizontalAlignment="Stretch">ModelVariantId</Label>
<TextBox Grid.Row="1" HorizontalAlignment="Stretch"
Text="{Binding ElementName=root, Path=CarModelFilter.ModelVariantId, Mode=TwoWay}" />
</Grid>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
There is no element named "root" in the same naming scope as the header, but you should be able to bind to a parent element in the visual tree using the RelativeSource property.
This assumes that the parent UserControl has a CarModelFilter property.

Putting ComboBox in a one special row in ListView in WPF

How to put a Combobox to a special row of a listview (WPF, VB.net)?
There is a ListView that has two columns. The columns are "Parameter" and "Values". The second column has different types (such as numbers, strings and "Selectable values") and is editable. The "Selectable values" have to appear in a Combobox.
One of the ways that I think about, is conditional XAML tags (I don`t know if it is possible or not ?) .
I tried this code but it shows Combobox for all rows in listview :
<ListView x:Name="listView"
HorizontalAlignment="Left"
Height="237"
Margin="40,34,0,0"
VerticalAlignment="Top"
Width="447"
ItemsSource="{Binding Values}">
<ListView.View>
<GridView>
<GridViewColumn Header="Parameter"
DisplayMemberBinding="{Binding Path=LName}"
Width="100" />
<GridViewColumn Header="Value"
Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=ItemsSource}"
Width="120">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Type}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I only want to use Combobox for one row while the type of the rest of rows are double or string. How can I do this?

How to set tabindex in dynamic generated textbox inside listView in wpf

We built our user interface from XML definitions (not XAML) but underneath we use a WPF to present the UI. That is at runtime, we create the WPF UI based on our XML definition.
<ListView ItemsSource="{Binding}" Width="400px" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" Name="ListView"
ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Grid.ColumnSpan="3"
SelectionChanged="ListView_SelectionChanged" BorderThickness="0" IsTabStop="False">
<ListView.View>
<GridView x:Name="grid">
<GridViewColumn Width="100px">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ModuleName}" Width="100px" Foreground="Black" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="200px">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=ModuleUserCount, TargetNullValue=''}" MaxLength="6" Name="txtModuleUserCount" KeyDown="txtModuleUserCount_KeyDown" MinWidth="180" MaxWidth="200" BorderBrush="Gray"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</ListView.View>
</ListView>
I generate 4 module through XML SO UI have four TextBox in these dynamic TextBox tabIndex is not working. I tried KeyboardNavigation.TabNavigation="Cycle" but it's not working. How to make tab navigation work for this layout?
I know its too too late, but I would like to answer. I had the same problem. I tried following way and it resolved my problem: Set:
KeyboardNavigation.TabIndex="10"
or whatever value you require in ListView itself.

CheckBox state of a CellTemplate in ListView

i am trying to use styles to prevent from repeting code, by putting them in a Resource Disctionary.
My question is, when we have a GridViewColumn in a ListView, which one of the columns have a DataTemplate, and in that DataTemplate we have the CellTemplate with only a CheckBox, can we bind the CheckBox state when the DataTemplete is in a ResourceDictionary?
What i have is this in my XAML:
<ListView Name="listView">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn DisplayMemberPath="{Binding [1]}"/>
<GridViewColumn DisplayMemberPath="{Binding [2]}"/>
<GridViewColumn DisplayMemberPath="{Binding [4]}"/>
<GridViewColumn DisplayMemberPath="{Binding [5]}"/>
<GridViewColumn DisplayMemberPath="{Binding [6]}"/>
<GridViewColumn DisplayMemberPath="{Binding [7]}"/>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsThreeState="False" IsChecked="{Binding [8]}" Unchecked="CheckBox_Changed" Checked="CheckBox_Changed"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
And i am trying to do something like this in the Resource Dictionary:
<DataTemplate x:Key="ListViewCheckboxCell">
<StackPanel>
<CheckBox IsThreeState="False" IsChecked="Make reference"/>
</StackPanel>
</DataTemplate>
And the values in that column is always a bool.
Thanks in advance!
What you did seems correct. You'll now have to write
<GridViewColumn CellTemplate="{StaticResource ListViewCheckboxCell}" />
The template will be taken into account, you can leave exactly the same template than the original in your resource dictionary: The binding is dynamically resolved, so when the XAML will be read, bindings will automatically be set to the related object, following what you indicated

WPF Listview - binding a text column to a combobox column selected item

I have a Listview:
<ListView ItemsSource="{Binding FieldMap.SourceTargetFieldMap, Mode=Default}">
<ListView.View>
<GridView>
<GridViewColumn Header="Source Fields" CellTemplate="{StaticResource sourceFieldsTemplate}"/>
<GridViewColumn Header="Source Values" CellTemplate="{StaticResource =sourceValueTemplate}">
<GridViewColumn Header="Target Field" CellTemplate="{StaticResource targetFieldTemplate}"/>
</GridView>
</ListView.View>
</ListView>
And I also have cell data templates that these bind to:
<DataTemplate x:Key="sourceFieldsTemplate">
<ComboBox x:Name="cbSourceField" SelectedValue="{Binding Path=SourceField, Mode=TwoWay}" DisplayMemberPath="FieldName" ItemsSource="{Binding SourceFieldValues}" Width="120" />
</DataTemplate>
<DataTemplate x:Key="sourceValueTemplate">
<TextBox x:Name="tbSourceValue" Margin="5,0,0,0" DataContext="{Binding ElementName=cbSourceField, Path=SelectedItem}" Text="{Binding Path=FieldValue, Mode=TwoWay}" TextWrapping="Wrap" Width="115" />
</DataTemplate>
<DataTemplate x:Key="targetFieldTemplate">
<TextBox x:Name="tbTargetField" Margin="5,0,0,0" Text="{Binding Path=TargetField}" TextWrapping="Wrap" Width="155" IsReadOnly="True"/>
</DataTemplate>
What I want to do is, bind the Source Value textbox field to the selected item of the SourceFields combobox. So when an item is selected from the sourcefield combobox, a property from the selectedItem (which is actually a object with two properties - something like fieldname and value) populates the textbox.
Both controls are "hidden" within datatemplates and I am not sure how to populate one from the other??
Any ideas? Much appreciated!!
Matt
You could insert a ContentConteol where content is bound to selected item of your listview - then the datetemplate should take care of displaying - I would set readonly on the textbox since altering the text will have no effect.

Resources