relativesource

Access parent DataContext from DataTemplate

流过昼夜 提交于 2019-11-26 11:10:30
I have a ListBox which binds to a child collection on a ViewModel. The listbox items are styled in a datatemplate based on a property on the parent ViewModel: <Style x:Key="curveSpeedNonConstantParameterCell"> <Style.Triggers> <DataTrigger Binding="{Binding Path=DataContext.CurveSpeedMustBeSpecified, ElementName=someParentElementWithReferenceToRootDataContext}" Value="True"> <Setter Property="Control.Visibility" Value="Hidden"></Setter> </DataTrigger> </Style.Triggers> </Style> I get the following output error: System.Windows.Data Error: 39 : BindingExpression path error:

Access parent DataContext from DataTemplate

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 02:51:13
问题 I have a ListBox which binds to a child collection on a ViewModel. The listbox items are styled in a datatemplate based on a property on the parent ViewModel: <Style x:Key=\"curveSpeedNonConstantParameterCell\"> <Style.Triggers> <DataTrigger Binding=\"{Binding Path=DataContext.CurveSpeedMustBeSpecified, ElementName=someParentElementWithReferenceToRootDataContext}\" Value=\"True\"> <Setter Property=\"Control.Visibility\" Value=\"Hidden\"></Setter> </DataTrigger> </Style.Triggers> </Style> I

RelativeSource binding from a ToolTip or ContextMenu

只谈情不闲聊 提交于 2019-11-26 02:21:57
问题 What am I doing wrong here?: <GridViewColumn> <GridViewColumn.CellTemplate> <DataTemplate> <Button> <Button.ToolTip> <TextBlock Text=\"{Binding Path=Title, RelativeSource={RelativeSource AncestorType=Window}}\" /> That\'s just a simplified example, that doesn\'t work anyway :) Actually I need to get a value from another property that is in scope of the Window\'s DataContext. Help me pls. 回答1: This is tricky because ToolTip is not part of the VisualTree. Here you see a cool solution for the

How do I use WPF bindings with RelativeSource?

蓝咒 提交于 2019-11-26 01:19:31
问题 How do I use RelativeSource with WPF bindings and what are the different use-cases? 回答1: If you want to bind to another property on the object: {Binding Path=PathToProperty, RelativeSource={RelativeSource Self}} If you want to get a property on an ancestor: {Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}} If you want to get a property on the templated parent (so you can do 2 way bindings in a ControlTemplate) {Binding Path=PathToProperty,

WPF TemplateBinding vs RelativeSource TemplatedParent

孤街浪徒 提交于 2019-11-26 00:46:02
问题 What is the difference between these 2 bindings: <ControlTemplate TargetType=\"{x:Type Button}\"> <Border BorderBrush=\"{TemplateBinding Property=Background}\"> <ContentPresenter /> </Border> </ControlTemplate> and <ControlTemplate TargetType=\"{x:Type Button}\"> <Border BorderBrush=\"{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}\"> <ContentPresenter /> </Border> </ControlTemplate> ? 回答1: TemplateBinding is not quite the same thing. MSDN docs are often written by