Xaml - send viewmodel property to method in another viewmodel - wpf

I have a datagrid bound to a collection of ViewModels that have a property called Distance. Is there a way in xaml to send that Distance property to a method in the ViewModel that the datagrid itemsource is on?
For example: (GetDistanceInKM would be on the same VM as the collection of reports)
<DataGrid ItemsSource="{Binding ReportViewModels}">
<DataGrid.Columns>
<DataGridTextColum Binding="{Binding Distance}" Header="Distance" EditingElementStyle="{StaticResource DistanceStyle}"/>
</DataGrid.Columns>
</DataGrid>
<Style x:Key="DistanceStyle" TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding={Binding GetDistanceInKM[Distance], Converter={StaticResource IsDistanceGreaterThanTen}} Value="True">
<Setter Property="BorderBrush" Value={StaticResource HighlightBorderBrush}"/>
</DataTrigger>
</Style.Triggers>
</Style>

You cannot bind to methods, only to properties. If you want to call a method when a property changes, do it in the setter of that property.
If i understood correctly you have two options:
When Distance is set, call GetDistanceInKM and modify a new property DistanceInKM. Then bind your DataTrigger to DistanceInKM using the converter as is now.
Bind your DataTrigger directly to Distance property and make the conversion to kms in a IsDistanceGreaterThanTenKMs Converter.

Related

Binding in Style Setter in DataTemplate WPF

Okay, I have a relatively involved problem. I'm trying to create a Window in WPF. The main element on this window is a DataGrid. Each one of the rows in the DataGrid has a DetailsPane which I set using DataGrid.RowDetailsTemplate. Depending on certain row-specific values, I need the DetailsPane to display different elements. To accomplish this I placed a ContentControl at the root of the DataTemplate and used a Style with DataTriggers to set its Content property. Now, inside one of these Setters is a ComboBox. This ComboBox needs to have its ItemsSource bound to a list, which is stored in a dependency property on the Window level (because its the same list regardless of the row). Below is a simplified version of what I'm looking at:
<Window>
...
<DataGrid>
...
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<ContentControl>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding RowSpecificBooleanProperty}" Value="False">
<Setter Property="Content">
<Setter.Value>
...
<ComboBox ItemsSource={HowDoIBindThisToTheWindowProperty}/>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Window>
So what I'm trying to figure out is how to bind the ItemsSource of that ComboBox to a dependency property of the top-level window. Andy idea how to accomplish that?
EDIT:
I should have mentioned this before but I've already tried using {RelativeSource AncestorType=Window} and ElementName in the binding. In both cases the list in the ComboBox is blank at runtime.
ItemsSource="{Binding WhateverList, RelativeSource={RelativeSource AncestorType=Window}}"

WPF ObservableCollection Item's

I have an ObservableCollection which is binded to a ListView:
private ObservableCollection<Cost> m_Costs;
public ObservableCollection<Cost> DatabaseRecords
{
get { return m_Costs; }
}
and
<ListView x:Name="ListView" ItemsSource="{Binding DatabaseRecords, UpdateSourceTrigger=PropertyChanged}">
When I add or remove item the UI will refresh, it's OK. But what if I change a property of an item in the collection? I have a DataTrigger which handle formatting items based on their property:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<DataTrigger Binding="{Binding Status, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Value="Removed">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
If I change the Status property the UI doesn't refresh.
m_Costs[0].Status = UIStatus.Removed;
Any idea how can I utilize the binding to refresh?
For your ObservableCollection you don't need to implement INotifyPropertyChanged, cause it's already observable.
But for your Status property you need to implement it, otherwise your View will not know that property was changed.

DataTrigger Overflow exception

How can this be an overflow exception...?
<DataTemplate x:Key="ElementTemplate">
<StackPanel Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Style.Triggers>
<DataTrigger Binding="{Binding Converter={StaticResource TypeConv}}" Value="{x:Type models:GroupModel}">
<Setter Property="Margin" Value="5 0 0 0"></Setter>
<Setter Property="DataContext" Value="{Binding Model}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<TextBlock Text="{Binding Name}"></TextBlock>
...
<StackPanel/>
<DataTemplate/>
For the reason: this is a template (with at least 25 UI contros) that normally needs Model A as datatype. the DataTemplate is a ListvVew ItemTemplate. But the datatype can be of type Model B. Model B has a property called 'Model', which is of type Model A.
So instead of copy pasting the whole block template and use style triggers or DataTemplate selectors, I just want to change the DataContext (from "{Binding}" to "{Binding Model}")
anyone has some suggestions, a solution?
Thx!
EDIT: the Converter returns the type of the incoming value (the data object itself). that way i can know when Model B is using the template and so to change the DataContext.
A work-around I would suggest to avoid possible recursion between setting the data context and triggering the DataTrigger:
Have both ModelA and ModelB implement a common interface called IListViewModel for example with a single property getter:
public interface IListViewModel
{
ModelA Model {get;}
}
Then, ModelA's implementation will return this, while ModelB's implementation returns this.ModelA The DataTemplate simply binds to the .Modelof whichever view model it's given.

Adding tool tip functionality to a wpf control that does not descend from FrameworkElement

I'm working on a custom class that descends from DataGridColumn. The base class for DataGridColumn is DependencyObject. As such, it does not have a Tooltip property.
I want my custom class to have a Tooltip property. Actually, I want it to also have a ToolTipTemplate property that is a DataTemplate that can be used to generate the ToolTip. How do I go about adding this functionality to my class?
Tony
Its a common misconception that DataGridColumn being a dependency object, is part of the visual tree. It is not. So even if we create an inheritable dependency property (just like DataContext or FlowDirection which automatically propagates down the visual parent to its child elements), the new property of ToolTip wont descend down to individual cells, as those cells are not the children of the data grid column.
So now that we know this, the only way left is to add a binding in the CellStyle and bind to the self Column.ToolTip property. Just because you have decided to go with ToolTipTemplate, then you could add a ContentControl and then bind to its content template.
Something like this...
<tk:DataGrid x:Name="MyDataGrid" RowHeaderWidth="15"
ItemsSource="{StaticResource MyData}"
AutoGenerateColumns="False">
<tk:DataGrid.CellStyle>
<Style TargetType="{x:Type tk:DataGridCell}">
<Setter Property="ToolTip">
<Setter.Value>
<ContentControl
ContentTemplate="{Binding Column.ToolTipTemplate,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type tk:DataGridCell}}}"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger
Binding="{Binding Column.ToolTipTemplate,
RelativeSource={RelativeSource Self}}"
Value="{x:Null}">
<Setter Property="ToolTip" Value="{x:Null}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</tk:DataGrid.CellStyle>
....
</tk:DataGrid>

FrameworkElement`s DataContext Property does NOT inherit down the element tree

Hello WPF Pros at least I hope some of you read this!
DataContext is a property on FrameworkElement (base class for all WPF Controls) and is implemented as a DependencyProperty. That means all the descendant elements in the logical tree share the same DataContext.
So the ContentControl should do it with its descendant elements right?
I have a scenario where that is NOT the case and I would like to know WHAT is the cause of that misbehaviour ?!
That you understand a bit more about it please read this thread ( dont NOT want to copy everything here) where the trouble starts...:
WPF: Can not find the Trigger target 'cc'. The target must appear before any Setters, Triggers
and to say it in short words: My DataTemplates within the ContentControl do have a dead DataContext that means there is NOTHING to bind to it, what is actually not possible...
Every Element down the ContentControl has NOTHING set in the DataContext Property ???
DataContext is a property on
FrameworkElement (base class for all
WPF Controls) and is implemented as a
DependencyProperty. That means all the
descendant elements in the logical
tree share the same DataContext.
The fact that it's a dependency property doesn't imply inheritance... It's true for DataContext, but only because the dependency property has the FrameworkPropertyMetadataOptions.Inherits flag in its metadata.
So the ContentControl should do it
with its descendant elements right?
ContentControl is a bit special: the DataContext of its descendants (the visual tree built from the DataTemplate) is actually be the Content of the ContentControl. So if your ContentControl has no content, the DataContext inside it is null.
This worked for me:
<ContentControl ContentTemplate="{StaticResource NotesTemplate}"
Content="{Binding}"
DataContext="{Binding HeightField}"/>
Without the Content="{Binding}", the DataContext was NULL
The last answer (from VinceF) worked for me too.
I wanted to show a usercontrol depending on the value of a property in my viewmodel. So I made a ContentControl with some Style Triggers. Depending on the value of a bind property the trigger sets a specific ContentTemplate containing the specific usercontrol.
The usercontrol was shown right, but its DataContext was always null. So I had to set the Context of the ContentControl to: Content="{Binding}" After that, the UserControls worked fine and had the same DataContext as their parent.
So my XAML looks like that:
In the Resources part I defined two DataTemplates; each one for each UserControl I want to show.
<DataTemplate x:Key="ViewA">
<namespace:UserControlA/>
</DataTemplate>
<DataTemplate x:Key="ViewB">
<namespace:UserControlB/>
</DataTemplate>
The part where I show the UserControl depending on a property is the following:
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Property}" Value="0">
<Setter Property="ContentControl.ContentTemplate" Value="{StaticResource ViewA}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Property}" Value="1">
<Setter Property="ContentControl.ContentTemplate" Value="{StaticResource ViewB}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
after reading this question and previous answers, I prefer using ContentControl with data triggered Content like this:
Controls which will be set as Content of ContentControl:
<TextBox x:Key="ViewA">
...
</TextBox>
<ComboBox x:Key="ViewB">
...
</ComboBox>
ContentControl which switch own content by DataTrigger in ContentControl style:
<ContentControl>
<ContentControl.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Property}" Value="0">
<Setter Property="Content" Value="{StaticResource ViewA}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Property}" Value="1">
<Setter Property="Content" Value="{StaticResource ViewB}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
I hope this helps to someone like previous answers to me.

Resources