当我们尝试将一个RelativeSource从xaml转到code behind时,原本好好的binding表达式居然出现binding错误。 UserControl x:Class="WpfApp1.UserControl1"> <Grid> <TextBlock Name="TextBlock" Text="{Binding <em>RelativeSource</em> ={<em>RelativeSource</em> AncestorType=Window},Path=Title}" /> </Grid> </UserControl> <Window x:Class="WpfApp1 ={<em>RelativeSource</em> AncestorType=Window},Path=Title,PresentationTraceSources.TraceLevel=High}" /> </ 但是此时发现RelativeSource (FindAncestor) requires tree context,我们在查找RelativeSource 时,需要有上下文树,而此时条件不满足,我们的绑定就被延迟了
={x:Static RelativeSource.TemplatedParent}}" Stretch="{Binding Stretch,RelativeSource={x:Static RelativeSource.TemplatedParent ={x:Static RelativeSource.TemplatedParent}}" Stretch="{Binding Stretch,RelativeSource={x:Static RelativeSource.TemplatedParent ={x:Static RelativeSource.TemplatedParent}}" Stretch="{Binding Stretch,RelativeSource={x:Static RelativeSource.TemplatedParent 第一种,绑定数据事件DataTrigger的条件时,使用RelativeSource.Self,如{Binding IsMouseOver,RelativeSource={x:Static RelativeSource.Self 第二种,条件成立,触发模板变化时,使用RelativeSource.TemplatedParent,如{Binding BackImage, RelativeSource={x:Static RelativeSource.TemplatedParent
Self模式 <Binding RelativeSource="{RelativeSource Self}" .../> 或 <object property="{Binding <em>RelativeSource</em> ={<em>RelativeSource</em> Self} ...}" .../> RelativeSource使用Self模式时, 目标对象将作为源对象绑定到自身。 TemplatedParent模式 <Binding RelativeSource="{RelativeSource TemplatedParent}" .../> 或 <object property ="{Binding <em>RelativeSource</em>={<em>RelativeSource</em> TemplatedParent} ...}" .../> RelativeSource使用TemplatedParent </Style> 在控件模板(ControlTemplate)中使用RelativeSource的TemplatedParent模式,”Binding RelativeSource={RelativeSource
在列表新建一个图标,添加 Visibility Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem} </Grid.ColumnDefinitions> <TextBlock Text="{Binding <em>RelativeSource</em> ={<em>RelativeSource</em> AncestorType=ListBoxItem}, Path=(ItemsControl.AlternationIndex),Mode=OneWay,Converter ={<em>RelativeSource</em> AncestorType=ListBoxItem}, Path=(ItemsControl.AlternationIndex),Mode=OneWay,Converter ={<em>RelativeSource</em> AncestorType=ListBoxItem}, Path=(ItemsControl.AlternationIndex),Mode=OneWay,Converter
toolkit\" "); cellTemp.Append(" >"); cellTemp.Append("<Grid Width=\"{Binding RelativeSource ={RelativeSource Mode=FindAncestor,AncestorType=toolkit:DataGridCell},Path=ActualWidth}\" "); cellTemp.Append(" Height=\"{Binding RelativeSource={RelativeSource AncestorType=toolkit:DataGridColumnHeader ={x:Static RelativeSource.Self}}\">"); cellTemp.Append(" <WrapPanel>"); ={x:Static RelativeSource.Self}}\">"); cellTemp.Append(" <WrapPanel>");
在列表新建一个图标,添加 Visibility Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem} </Grid.ColumnDefinitions> <TextBlock Text="{Binding <em>RelativeSource</em> ={<em>RelativeSource</em> AncestorType=ListBoxItem}, Path=(ItemsControl.AlternationIndex),Mode=OneWay,Converter 获取当前列表项,使用{Binding <em>RelativeSource</em>={<em>RelativeSource</em> AncestorType=ListBoxItem}, Path=(ItemsControl.AlternationIndex ={<em>RelativeSource</em> AncestorType=ListBoxItem}, Path=(ItemsControl.AlternationIndex),Mode=OneWay,Converter
={RelativeSource Self},Path=Height}"/> 绑定祖先元素属性 如果想让图片上下填充满,左右等比自适应,可以使用下面的方式实现: <Border BorderBrush= "#434343" BorderThickness="1"> <Grid> <Image Height="{Binding ActualHeight, <em>RelativeSource</em> 其中 <em>RelativeSource</em>={<em>RelativeSource</em> AncestorType={x:Type Grid}} 是用来指定查找最近类型为Grid的祖先元素。 ={<em>RelativeSource</em> TemplatedParent}, Path=Background}" /> <ContentPresenter Margin="35" Content ="{Binding <em>RelativeSource</em>={<em>RelativeSource</em> TemplatedParent}, Path=Content}" /> </Canvas> <
={RelativeSource TemplatedParent}}" ColumnHeaderContainerStyle ={RelativeSource TemplatedParent}}" ColumnHeaderTemplate="{ Binding Path=TemplatedParent.View.ColumnHeaderTemplate, RelativeSource Path=TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource Path=TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource
I modified the ContentPresenter in the following way in order to test out using the "FindAncestor" RelativeSource ={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True" ={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="False ={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"> ={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True
> <Grid x:Name="MainGrid"> <drawing:PanningItems ItemsSource="{Binding stringCollection,<em>RelativeSource</em> ={<em>RelativeSource</em> AncestorType=local:MainWindow}}" x:Name="MyPanningItems ={<em>RelativeSource</em> AncestorType=local:MainWindow}}"/> <TextBlock Text=":" x:Name="PART_TextBlock ={<em>RelativeSource</em> AncestorType=local:MainWindow}}"/> </WrapPanel> <TextBlock Grid.Row ="1" FontSize="45" HorizontalAlignment="Center" Text="{Binding Date,<em>RelativeSource</em>={<em>RelativeSource</em> AncestorType
={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/> </StackPanel> </DataTemplate ={RelativeSource TemplatedParent}}" VerticalAlignment="{TemplateBinding VerticalContentAlignment ={RelativeSource Mode=FindAncestor,AncestorType=ListBox},Path=SelectionMode}" Value="Single ={RelativeSource Mode=FindAncestor,AncestorType=ListBox},Path=IsMultiSelectCheckBoxEnabled}" ={RelativeSource AncestorType={x:Type DataGridRow}, Mode=FindAncestor}}" x:Name
解决方法很简单,使用RelativeSource找到数据。 只需要修改<Binding Path="DataContext.Property2" RelativeSource="{RelativeSource AncestorType=DataGridCell} <MultiBinding > <Binding Path="DataContext.Property1" RelativeSource ="{RelativeSource AncestorType=DataGridCell}"/> <Binding Path="DataContext.Property2 " RelativeSource="{RelativeSource AncestorType=DataGridCell}"/> <MultiBinding.Converter
DataContext绑定 RelativeSource绑定 集合当前项绑定 1、DataContext绑定 DataContext是一个依赖属性,它是绑定的默认源。 输出 2、RelativeSource 绑定 RelativeSource是一个属性,它用相对关系设置绑定源以绑定目标。此扩展主要用于必须将元素的一个属性绑定到同一元素的另一个属性时。 RelativeSource有四种类型,如下所示。 Self FindAncestor TemplatedParent PreviousData 让我们一个一个详细地探讨一下。 <Grid> <Ellipse Fill="Black" Height="100" Width="{Binding <em>RelativeSource</em>={<em>RelativeSource</em> Self}, Margin="35" Content="{Binding <em>RelativeSource</em>={<em>RelativeSource</em> TemplatedParent},Path=Content
Margin="5" Command="{Binding DataContext.ClickAcceptCommand, RelativeSource ={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding}" ={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding}" Margin="5" Command="{Binding DataContext.ClickAgreeCommand, RelativeSource ={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding}"
HorizontalAlignment="{Binding Path=HorizontalContentAlignment, RelativeSource ={RelativeSource AncestorType={x:Type ItemsControl}}}" VerticalAlignment ="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl ={RelativeSource TemplatedParent}}" Style="{StaticResource WD.NormalButton ={RelativeSource Self}}" /> </TabItem.Header> <Rectangle Fill="{DynamicResource
={RelativeSource TemplatedParent}, Path=TemplateSettings.MaxSideLength}" MaxHeight="{Binding RelativeSource ={RelativeSource TemplatedParent}, Path=TemplateSettings.MaxSideLength}" Visibility="Collapsed" ={RelativeSource TemplatedParent}, Path=TemplateSettings.EllipseDiameter}" Height="{Binding RelativeSource ={RelativeSource TemplatedParent}, Path=TemplateSettings.EllipseDiameter}" Margin="{Binding RelativeSource ={RelativeSource TemplatedParent}, Path=TemplateSettings.EllipseOffset}" Fill="{TemplateBinding
大神问,如何在 ListView 绑定前一项,于是我下面告诉大家如何在 ListView 绑定前一项 WPF 绑定前一项 可以使用绑定的 RelativeSource 就可以绑定前一项,请看代码 <Style.Triggers> <DataTrigger Binding="{Binding <em>RelativeSource</em> ={<em>RelativeSource</em> PreviousData}}"> </DataTrigger <TextBlock x:Name="上一个的" Margin="10,100,10,10" Text="" local:Foo1.Foo="{Binding <em>RelativeSource</em> ={<em>RelativeSource</em> Self}}"></TextBlock> </Grid> 然后把代码写到 FooPropertyChangedCallback 就可以了
={RelativeSource TemplatedParent}}"> <ContentPresenter x:Name="PART_ContentPresenter ={<em>RelativeSource</em> TemplatedParent}}"/> <TextBox Grid.Column="2" x:Name=" ={<em>RelativeSource</em> AncestorType=Button}}" Data="{StaticResource PathPrevious}" ="{<em>RelativeSource</em> TemplatedParent}"/> <Binding Path="PageCount" RelativeSource ={RelativeSource TemplatedParent}}"/> <StackPanel Grid.Column="1" Orientation
={RelativeSource AncestorType={x:Type DataGridRow}}}" Visibility= "{Binding (Validation.HasError), Converter={StaticResource bool2VisibilityConverter}, RelativeSource= ={RelativeSource AncestorType={x:Type DataGrid}}}" /> </SelectiveScrollingGrid ={RelativeSource AncestorType={x:Type DataGrid}}}" /> </Trigger> </Style.Triggers ={RelativeSource Mode=TemplatedParent}}" /> <Grid Grid.Row
I have tried setting the DataContext="{Binding RelativeSource={RelativeSource Self}}" (instead of setting 我尝试了为 Xaml 中绑定到 Color 属性的元素设置 DataContext="{Binding RelativeSource={RelativeSource Self}}" (而不是在后台代码中设置 Instead, on the binding of the GradientStop, you can use RelativeSource={RelativeSource AncestorType= 取而代之的是,在绑定 GradientStop 时,你可以使用 RelativeSource={RelativeSource AncestorType=Bin} (假设 Bin 是你的控件)。 ={RelativeSource TemplatedParent}}" /> </ControlTemplate> <MyTimePicker.Template> </MyTimePicker