Questions tagged «relativesource»


6
WPF TemplateBinding与RelativeSource TemplatedParent
是什么 两个绑定之间有区别: <ControlTemplate TargetType="{x:Type Button}"> <Border BorderBrush="{TemplateBinding Property=Background}"> <ContentPresenter /> </Border> </ControlTemplate> 和 <ControlTemplate TargetType="{x:Type Button}"> <Border BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}"> <ContentPresenter /> </Border> </ControlTemplate> ?

6
从DataTemplate访问父DataContext
我有一个ListBox绑定到ViewModel上的子集合。列表框项是根据父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> 我收到以下输出错误: System.Windows.Data Error: 39 : BindingExpression path error: 'CurveSpeedMustBeSpecified' property not found on 'object' ''BindingListCollectionView' (HashCode=20467555)'. BindingExpression:Path=DataContext.CurveSpeedMustBeSpecified; DataItem='Grid' (Name='nonConstantCurveParametersGrid'); target element is 'TextBox' (Name=''); target property is 'NoTarget' (type 'Object') 因此,如果我将绑定表达式更改为"Path=DataContext.CurrentItem.CurveSpeedMustBeSpecified"它可以工作,但前提是父用户控件的datacontext是a BindingListCollectionView。这是不可接受的,因为其余的用户控件会自动绑定到CurrentItemon的属性BindingList。 如何在样式内部指定绑定表达式,以便无论父数据上下文是集合视图还是单个项目,绑定表达式都可以工作?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.