在WPF中如何使用RelativeSource绑定

在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource。

这种办法的意思是指当前元素和绑定源的位置关系。

第一种关系: Self

举一个最简单的例子:在一个StackPanel中,有一个TextBlock。

<TextBlock FontSize="18" FontWeight="Bold" Margin="10"
                 Background="Red" Width="80" Height="{Binding RelativeSource={RelativeSource Self},Path=Width}">MultiBinding Sample</TextBlock>

如果想让textbox的width和height相同,通过设置属性Height="{Binding RelativeSource={RelativeSource Self},Path=Width}" 就可以实现。

第二种关系:TemplatedParent

例如为一个Button写一个样式,修改Button为椭圆型。同时需要椭圆的背景色和Button的背景色相同。

<Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Green"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Ellipse>
                                <Ellipse.Fill>
                                    <SolidColorBrush Color="{Binding Path=Background.Color,RelativeSource={RelativeSource TemplatedParent}}"/>
                                </Ellipse.Fill>
                            </Ellipse>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

 在这个例子中 TemplateParent就是指的Button

第三种关系:AncestorType

指定绑定源为某个父元素

<Grid>

          <Label Background = {Binding Path=Background, RelativeSource={RelativeSource AncestorType={x:Type Grid}}}/>

     </Grid>

这个例子中Label的背景色和Grid的背景色一样。

时间: 2024-08-09 22:01:42

在WPF中如何使用RelativeSource绑定的相关文章

WPF中一个控件绑定另一个控件的属性

原文:WPF中一个控件绑定另一个控件的属性 如同一个Grid中的一个按钮根据另一个按钮的显示与否作出不同的响应: 绑定的时候通过ElementName来指定控件 <Grid Margin="50,130"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="40"/> </Grid.ColumnDefinitions>

WPF中的Command事件绑定

在项目中使用Command绑定能够使我们的代码更加的符合MVVM模式.不了解的同学可能不清楚,只有继承自ButtonBase类的元素才可以直接绑定Command(Button.CheckBox.RadioButton等) <Button Content="Normal" Command="{Binding NormalEventCommand}" ></Button> 如果我们要处理Label或者其他的一些控件,那么只能在走事件: <L

WPF中多个RadioButton绑定到一个属性

如图样: 在View中: <RadioButton IsChecked="{Binding Option, Converter={cvt:EnumToBooleanConverter},ConverterParameter={x:Static enum:RadionButtonOptions.One}}" Content="One" DockPanel.Dock="Top"/> <RadioButton IsChecked=&q

整理:WPF中Binding的几种写法

目的:整理WPF中Bind的写法 <!--绑定到DataContext--> <Button Content="{Binding DataTime}"/> <!--绑定到DataContext,并设置绑定模式--> <Button x:Name="btn" Content="{Binding DataTime,Mode=OneTime}"/> <!--绑定到DataContext,并设置更新模

利用Powershell快速对WPF的ListBox 数据源的绑定

有时候,我们在封闭的环境中做开发工作,并没有那么多便捷的开发工具,只能利用当前系统自带的基本工具做开发, 比如利用强大的framework,而powershell就是很好的开发工具,在不安装visual studio的情况下,就能实现大部分功能. 下面就用一个简单的例子来实现WPF中ListBox 数据源的绑定 1.关于dataset,我就不做多解释了,简单来说,就是一个或多个DataTable 对象的集合. 用powershell来实现的话,是简单不过的事情了. #Create Table o

ComboBox在WPF中的绑定示例:绑定项、集合、转换,及其源代码

在WPF的Xaml中为ComboBox绑定数据时,由于参数很多,很容易混淆,在ListView中使用更是如此.本文通过对ComboBox在窗口和在ListView中绑定对象的属性和属性可能是枚举类型的情况进行简单讲解和示例,以作实际应用参照. 源码可以到这里下载:ComboBoxBindings.rar 1.ComboBox在窗口容器中的情况 2.ComboBox在ListView中的情况 3.绑定枚举 示例中做枚举类型Sex的绑定时,先在Xaml中绑定值,然后在ComboBox的ItemsSo

wpf中如何在xaml中绑定cs中类的属性

cs代码:/// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ContentGrid.DataContext = this; this.Path = "数据绑定"; } public string Path { get; set;

MVVM模式解析和在WPF中的实现(三) 命令绑定

MVVM模式解析和在WPF中的实现(三) 命令绑定 0x00 命令绑定要达到的效果 命令绑定要关注的核心就是两个方面的问题,命令能否执行和命令怎么执行.也就是说当View中的一个Button绑定了ViewModel中一个命令后,什么时候这个Button是可用的,按下Button后执行什么操作.解决了这两个问题基本就实现了命令绑定.另外一个问题就是执行过程中需要的数据(参数)要如何传递.本次主要探讨这几个问题. 0x01 命令绑定的实现 自定义一个能够被绑定的命令需要实现ICommand接口.该接

转:WPF中ListBox的创建和多种绑定用法

先从最容易的开始演示ListBox控件的创建. Adding ListBox Items下面的代码是向ListBox控件中添加多项ListBoxItem集合.XAML代码如下:<ListBox Margin="10,10,0,13" Name="listBox1" HorizontalAlignment="Left"         VerticalAlignment="Top" Width="194"