如何使用RelativeSourceWPF绑定以及不同的用例?
这两个绑定有什么区别:
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderBrush="{TemplateBinding Property=Background}">
<ContentPresenter />
</Border>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
和
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}">
<ContentPresenter />
</Border>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
?
我有一个ListBox绑定到ViewModel上的子集合.列表框项基于父ViewModel上的属性在datatemplate中设置样式:
<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>
Run Code Online (Sandbox Code Playgroud)
我收到以下输出错误:
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')
Run Code Online (Sandbox Code Playgroud)
因此,如果我将绑定表达式更改为"Path=DataContext.CurrentItem.CurveSpeedMustBeSpecified"它可以工作,但只要父用户控件的datacontext是a BindingListCollectionView.这是不可接受的,因为用户控件的其余部分会自动绑定到CurrentItemon的属性BindingList.
如何在样式中指定绑定表达式,以便无论父数据上下文是集合视图还是单个项目,它都可以工作?
我在这做错了什么?:
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button>
<Button.ToolTip>
<TextBlock Text="{Binding Path=Title, RelativeSource={RelativeSource AncestorType=Window}}" />
Run Code Online (Sandbox Code Playgroud)
这只是一个简单的例子,无论如何都不起作用:)实际上我需要从Window的DataContext范围内的另一个属性中获取一个值.
帮帮我吧
我有ItemsControl绑定到Student类型的集合.在ItemTemplate中我有一个TextBox,它使用IValueConverter来做一些自定义计算和逻辑.我想将实际的Student对象传递给值转换器,而不是它的属性.我怎样才能做到这一点?这是我的代码示例.
<ItemsControl ItemsSource="{Binding StudentList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding ????, Converter={StaticResource MyConverter}}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
在代码我有这个
public class MyValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// I want 'value' to be of type Student.
return null;
}
}
Run Code Online (Sandbox Code Playgroud) 我目前在WPF用户控件(我的XAML文件的根元素是"UserControl")中工作,我知道它是在Window中托管的.如何使用数据绑定访问Window的属性?
有谁知道为什么简单
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}" Path="..." />
Run Code Online (Sandbox Code Playgroud)
不起作用?我得到的错误信息是:
System.Windows.Data警告:4:无法找到绑定源,引用'RelativeSource FindAncestor,AncestorType ='System.Windows.Window',AncestorLevel ='1''.
编辑:我最终使用了ArsenMkrt方法的变体,因此接受了他的回答.但是,我仍然有兴趣找出为什么FindAncestor不"正常工作".
在Silverlight 4中会有RelativeSource FindAncestor,AncestorType ......吗?
以下哪些TextBlocks的绑定会降低性能:
<Window
x:Name="Me"
x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfApplication1"
Title="MainWindow">
<StackPanel>
<TextBlock Text="{Binding Title, ElementName=Me}"/>
<TextBlock Text="{Binding Title, RelativeSource={RelativeSource AncestorType={x:Type src:MainWindow}}}"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
我确信当TextBlocks处于具有许多兄弟和祖先的高嵌套级别时,我的问题可能会有所不同.
(仅基于个人想法,我可能在每个特定的人中都错了!):
ElementName:
Name控件的属性应该比调用成本低GetType.Name设置时.FindAncestor:
GetType确定祖先类型; GetType比简单的Name属性获取器(可能DP不同?)的性能更高我一直在尝试构建一个带有提示的文本框,当它显示为空时显示.我在一个样式中设置提示文本时遇到问题.
确切地说,这是有效的(也就是说,它正确绑定):
<TextBox Tag="hint text">
<TextBox.Background>
<VisualBrush Stretch="None">
<VisualBrush.Visual>
<TextBlock Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=TextBox}}" FontStyle="Italic" Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</TextBox.Background>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
但是,当我将它移动到Style时,它不会:
<Style TargetType="TextBox" x:Key="stlHintbox">
<Style.Triggers>
<DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Mode=Self}}" Value="">
<Setter Property="Background">
<Setter.Value>
<VisualBrush Stretch="None">
<VisualBrush.Visual>
<TextBlock Tag="inner" Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=TextBox}}"
FontStyle="Italic" Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<TextBox Tag="hint text" Style="{StaticResource stlHintbox}" />
Run Code Online (Sandbox Code Playgroud)
那捕获的是什么?如何从样式中绑定祖先属性?
通过名称进行绑定自引用与通过RelativeSource进行自引用之间有什么区别?
例如:
<!-- Self referential via name -->
<Window ...
x:Name="This"
DataContext="{Binding Path=Data, ElementName=This}"/>
Run Code Online (Sandbox Code Playgroud)
与
<!-- Self referential via RelativeSource -->
<Window ...
x:Name="This"
DataContext="{Binding Path=Data, RelativeSource={RelativeSource Self}}"/>
Run Code Online (Sandbox Code Playgroud)
在我的项目中,它们看起来表现相同,但我在窗口中直接使用它们.这意味着我立刻更喜欢前者,因为它输入较少.
RelativeSource的唯一优势是它能够在(例如)广泛使用的风格中自我引用吗?还是有一些额外的语义差异?
relativesource ×10
wpf ×9
binding ×4
data-binding ×4
xaml ×4
.net ×2
elementname ×2
datatemplate ×1
findancestor ×1
parent ×1
performance ×1
silverlight ×1
styles ×1