Databinding to Property of class that in another class with property - wpf

What is the correct way to perform databinding if the property (the context in this case) that i bind to is a class (LatestReading class) with its property (Unit).
The datacontext is set to the root of the class. Here's the hierarchy of my class. MonitoringPoint.LatestReading.Unit
<DataTemplate x:Key="TrackBallInfoTemplate"
DataType="{x:Type winGRAPH_Core:MonitoringNode}">
<StackPanel Margin="3">
<Grid Height="Auto">
<StackPanel Height="Auto"
VerticalAlignment="Top"
Orientation="Horizontal">
<TextBlock TextWrapping="Wrap"
Text="{Binding DataPoint.Value, FallbackValue=0.00}"
FontWeight="Bold"
FontSize="16" />
<TextBlock TextWrapping="Wrap"
Text="{Binding LatestReading.Unit, FallbackValue=°C}"
Margin="2,3,0,0" />
</StackPanel>
</Grid>
<Grid Height="Auto">
<TextBlock TextWrapping="Wrap"
Text="{Binding DataPoint.Category, FallbackValue=time}"
d:LayoutOverrides="Width, Height"
FontStyle="Italic"
Margin="0,2,0,0" />
</Grid>
</StackPanel>
</DataTemplate>

If the type that is assigned to your DataContext is a LatestReading then just use the property Unit. The DataContext already set at a higher level (if you've remembered to set it - it's a common mistake to forget) automatically cascades to the child controls.

Related

Overriding parent control's datacontext with local datacontext

I have a Groupbox in which i have multiple Textboxes. All these Textbox derive their Datacontext from that of Groupbox but one of the Textbox in the group needs a different Datacontext.
<GroupBox Header="My Group" Height="150" Width="1132" DataContext="{Binding ContextA}" >
<Grid>
<Label x:Name="lblA" Content="Policy Number:" Margin="6,12,970,92" />
<TextBox x:Name="txtbA" Margin="155,12,0,0" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" Text="{Binding ValueA}" VerticalAlignment="Top" Width="278" Grid.ColumnSpan="2"/>
<Label x:Name="lblB" Content="Policy Type:" Margin="612,10,334,88" Height="30"/>
<TextBox x:Name="txtbB" Margin="801,12,0,0" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" DataContext="{Binding ContextB}" Text="{Binding ValueB}" VerticalAlignment="Top" Width="278"/>
</Grid>
</GroupBox>
In the above code txtbA uses the Datacontext same as that of Groupbox.
I want txtbB to have a separate Datacontexti.e. ContextB
But the ContextB is not getting assigned to txtbB. How to solve the problem?
Note:
ContextAand ContextB= list of Entity Framework models.
WPF binding engine look for property in current DataContext. So, in your case binding engine is looking for property ContextB in class ContextA since textBox is inheriting DataContext from parent GroupBox.
What you can do is use more verbose definition for ContextA like this:
<GroupBox Header="My Group" Height="150" Width="1132"
DataContext="{Binding}"> <-- HERE Or can remove setting DC altogether.
<Grid>
<Label x:Name="lblA" Content="Policy Number:" Margin="6,12,970,92" />
<TextBox x:Name="txtbA" Margin="155,12,0,0" HorizontalAlignment="Left"
Height="24" TextWrapping="Wrap"
Text="{Binding ContextA.ValueA}" <-- HERE
VerticalAlignment="Top"
Width="278" Grid.ColumnSpan="2"/>
<Label x:Name="lblB" Content="Policy Type:" Margin="612,10,334,88"
Height="30"/>
<TextBox x:Name="txtbB" Margin="801,12,0,0" HorizontalAlignment="Left"
Height="24"
TextWrapping="Wrap" DataContext="{Binding ContextB}"
Text="{Binding ValueB}" VerticalAlignment="Top" Width="278"/>
</Grid>
</GroupBox>

How can I bind radiobutton's checked property inside DataTemplate?

This is my DataTemplate:
<DataTemplate x:Key="DataTemplateThemes">
<Grid Width="160" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5,0">
<Grid.RowDefinitions>
<RowDefinition Height="28.3336664835612"/>
<RowDefinition Height="161"/>
<RowDefinition Height="Auto" />
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock x:Name="tbThemeName" HorizontalAlignment="Left" Margin="2" Grid.Row="2" TextWrapping="Wrap"
Text="{Binding ThemeName}" FontSize="16" FontFamily="Segoe UI" d:LayoutOverrides="Height"/>
<Image Margin="0" Stretch="Fill" Grid.Row="1" Source="{Binding Path=ThemePicName, Converter={StaticResource imageConverter}}"/>
<TextBlock x:Name="tbDescription" HorizontalAlignment="Left" Margin="2" Grid.Row="3"
TextWrapping="Wrap" Text="{Binding ThemeDescription}" FontSize="14.667" FontFamily="Segoe UI"
d:LayoutOverrides="Height"/>
<RadioButton Content="Apply" Margin="-10,0,0,0"
d:LayoutOverrides="Width, Height" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
The IsChecked property of RadioButton depends on Theme property which is available in my ViewModel. Initially I thought I will create a Converter that will pass the current row (datatemplate current DataContext)'s ThemeId and ViewModel's Theme (which is the selected Theme)'s ThemeId property and if they both match I would check the radiobutton. But ConverterParameter cannot take Binding expression as it is not a DependencyProperty. So how do I solve this issue?
Thanks in advance :)
You could implement a multi binding solution. I have previously implemented the solution described here:
http://www.scottlogic.co.uk/blog/colin/2010/05/silverlight-multibinding-solution-for-silverlight-4/
You could then have a converter that accepts the two required parameters.

Binding inside listbox itemtemplate problems

I have two separate binding problems with listboxes with an itemtemplate which contains a texbox.
1) One listbox binds to a list of strings. How can I display each string inside the textboxes created and allow two way binding at the same time? Two way binding isn't allowed without specifying a Path or XPath.
<ListBox Height="231" HorizontalAlignment="Left" Margin="0,167,0,0" Name="listBoxKeys" VerticalAlignment="Top" Width="219" ItemsSource="{Binding Path=SelectedPlatform.Keys}" SelectedItem="{Binding Path=SelectedKey,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<TextBox Text="{Binding Mode=OneWay}" Margin="0,0,0,0" Height="Auto" MinWidth="80" MaxWidth="80" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And 2) I use another listbox which binds to some generic list of a custom KeyValuePair class. The itemtemplate contains a textbox and combobox. The textbox text is bound to the key property of each KeyValuePair object and the combobox selecteditem to the value property. My problem is that I want the combo to get filled by a list of strings declared in my viewmodel which will be changing on runtime. The window's datacontext is the viewmodel where the list is declared. I don't know the exact syntax I need to use to bind the combobox itemssource there. Here's my code :
<ListBox Height="393" HorizontalAlignment="Left" Margin="0,72,0,0" Name="listBoxActions" VerticalAlignment="Top" Width="254" ItemsSource="{Binding Path=SelectedPlayer.ControlProfile.MappedActions}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<TextBox Text="{Binding Key, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Margin="10,0,0,0" Height="Auto" MinWidth="80" MaxWidth="80" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<ComboBox Margin="10,0,0,0" Height="Auto" MinWidth="80" MaxWidth="80" HorizontalAlignment="Left" VerticalAlignment="Center" ItemsSource="{Binding ?}" SelectedItem="{Binding Value, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The problem is that the two-way binding on the source itself cannot work because it would mean that the whole object (string), for which the data template is created, must be replaced when user changes the text in the text box. Obviously, this will not work. Two-way binding will work only on a writable property of the bound object.
In your case I would suggest creating a view model for the items in the list box (basically a view model for your strings) and expose a Value property on it and bind to it in the data template:
<ListBox Height="231" HorizontalAlignment="Left" Margin="0,167,0,0"
Name="listBoxKeys" VerticalAlignment="Top" Width="219"
ItemsSource="{Binding Path=SelectedPlatform.KeyViewModels}"
SelectedItem="{Binding Path=SelectedKey,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<TextBox Text="{Binding Value, Mode=TwoWay}" Margin="0,0,0,0" Height="Auto" MinWidth="80" MaxWidth="80" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
1) Pavlo Glazkov seems to have a good answer to me
2) This is down to the DataContext for the ComboBox now being the key value pair rather than the ViewModel. There may be other ways to do this but the one that I've used before is to set the bindings RelativeSource source back to it's parent ItemsControl.
RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}
Something like:
<ListBox Height="393" HorizontalAlignment="Left" Margin="0,72,0,0" Name="listBoxActions" VerticalAlignment="Top" Width="254" ItemsSource="{Binding Path=SelectedPlayer.ControlProfile.MappedActions}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<TextBox Text="{Binding Key, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Margin="10,0,0,0" Height="Auto" MinWidth="80" MaxWidth="80" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<ComboBox Margin="10,0,0,0" Height="Auto" MinWidth="80" MaxWidth="80" HorizontalAlignment="Left" VerticalAlignment="Center" ItemsSource="{Binding DataContext.Keys, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" SelectedItem="{Binding Value, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

How to reuse contents in wpf/mvvm

I have a UI that displays a pattern of "first name/last name". So I thought I would reuse the same template. But I am facing some issues getting the binding right.
Note:-
PrimaryContactDataContext is nothing but a class, with a property named "value" which implements the *INotifyPropertyChanged" interface.
<StackPanel>
<ContentControl DataContext="{Binding Path=PrimaryContactDataContext.Value,Mode=TwoWay}" ContentTemplate="{StaticResource PersonalDetailsTemplate}" />
</StackPanel>
// See the Reusable template below
<UserControl.Resources>
<DataTemplate x:Key="PersonalDetailsTemplate" >
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Width="30" Text="Name"></TextBlock>
<TextBox Width="110" Text="{Binding LastName}" IsReadOnly="True"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Width="30" Text="Title"></TextBlock>
<TextBox Width="110" Text="{Binding firstName}" IsReadOnly="True"></TextBox>
</StackPanel>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
Set the Content of the ContentControl, not its DataContext:
<ContentControl Content="{Binding Path=PrimaryContactDataContext.Value,Mode=TwoWay}" ContentTemplate="{StaticResource PersonalDetailsTemplate}" />

ItemsSource on wpf ComboBox getting reset?

I have a ComboBox setup in xaml and have set the ItemsSource binding. When I run the project nothing shows up in the ComboBox. If I inspect it with snoop the ItemsSource of the ComboBox is blank.
Anyone come across this before?
I checked the binding errors this is the error it displays
System.Windows.Data Error: 39 : BindingExpression path error: 'WasteTypeData' property not found on 'object' ''JobItems' (HashCode=28494546)'. BindingExpression:Path=WasteTypeData; DataItem='JobItems' (HashCode=28494546); target element is 'ComboBox' (Name='CboWasteTypes'); target property is 'ItemsSource' (type 'IEnumerable')
WasteTypeData is a public property of ObservableCollection<WasteTypes>.
This is what I have set as the binding of the ComboBox and if I debug the app WasteTypeData is populated with the list of WasteTypes as expected.
I can't figure out why it's looking for WasteTypeData on object JobItems. The WasteTypeData property is not found on the object JobItems.
JobItemsData is a public property of ObservableCollection<JobItems>.
My xaml has a ListBox with its ItemsSource Binding set to JobItemsData.
The ListBox has a DataTemplate with a couple of TextBoxes and one ComboBox. All the TextBoxes display their data properly.
Here's xaml if it will help shed any light on what's going on:
<UserControl
x:Class="WorkItems.View.ViewJobItems"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:JobItemsViewModel="clr-namespace:WorkItems.ViewModel"
Height="300" Width="500">
<ListBox
x:Name="LstJobItems"
ItemsSource="{Binding JobItemsData}"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel
Grid.Column="0"
Margin="5">
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Customer Details"
FontWeight="Bold"
FontSize="24"></Label>
</StackPanel>
<StackPanel
Orientation="Horizontal">
<Line
StrokeThickness="3"></Line>
</StackPanel>
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Customer: "
FontWeight="Bold"
Width="110" />
<TextBox
Text="{Binding Customer, Mode=OneWay}"
Width="200" />
</StackPanel>
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Address: "
FontWeight="Bold"
Width="110" />
<TextBox
Text="{Binding Address1, Mode=OneWay}"
Width="200" />
</StackPanel>
<StackPanel
Grid.Column="1"
Margin="5">
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Job Details"
FontWeight="Bold"
FontSize="24"></Label>
</StackPanel>
<StackPanel
Orientation="Horizontal">
<Line
StrokeThickness="3"></Line>
</StackPanel>
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Date: "
FontWeight="Bold"
Width="110" />
<TextBox
Text="{Binding JobDate, Mode=OneWay}"
Width="200" />
</StackPanel>
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Waste Type: "
FontWeight="Bold"
Width="110" />
<ComboBox
x:Name="CboWasteTypes"
IsEditable="False"
ItemsSource="{Binding Path=WasteTypeData}"
DisplayMemberPath="WasteType"
SelectedValuePath="WasteTypeID"
SelectedValue="{Binding WasteTypeID}"
Width="200" />
</StackPanel>
<StackPanel
Orientation="Horizontal"
Margin="0,5,0,0">
<Label
Content="Status: "
FontWeight="Bold"
Width="110" />
<TextBox
Text="{Binding Status, Mode=OneWay}"
Width="200" />
</StackPanel>
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</UserControl>
Thanks
Paul
Check the Output window for any binding errors. You may have misspelled something or not set the DataContext correctly.
I think its failing because when you use {Binding Path=WasteTypeData} in your combobox, it expects to find it as a property in JobsItems instead of the observable collection, since that is what the parent control (your ListBox) is bound to.
Add WasteTypeData as a static resource in your user control, then bind your combobox to that, specifying it using "{Binding Source={StaticResource..."
<UserControl
...
xmlns:local="WorkItems"
...
Height="300" Width="500">
<UserControl.Resources>
<local:WasteTypeData x:Key="WasteTypeData"/>
</UserControl.Resources>
..
<ComboBox
x:Name="CboWasteTypes"
IsEditable="False"
ItemsSource="{Binding Source={StaticResource WasteTypeData}}"
DisplayMemberPath="WasteType"
SelectedValuePath="WasteTypeID"
SelectedValue="{Binding WasteTypeID}"
Width="200" />
See if that helps!

Resources