ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

0517

2019-05-17 17:56:49  阅读:336  来源: 互联网

标签:控件 0517 Self 绑定 Binding RelativeSource AncestorType


<Window x:Class="_6_27.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="g1" Background="Red" Margin="10">
        <DockPanel Name="d1" Background="Orange" Margin="10">
            <Grid x:Name="g2" Background="Yellow" Margin="10">
                <DockPanel Name="d2" Background="LawnGreen" Margin="10">
                    <TextBox Name="textbox" FontSize="24" Margin="10"
                             Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid},AncestorLevel=1},Path=Name}"/>
                </DockPanel>
            </Grid>
        </DockPanel>
    </Grid>
</Window>
 

关联控件本身的属性时,只需要设置RelativeSource的Mode=Self即可。

控件关联其父级容器的属性 Mode=FindAncesto

Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid},AncestorLevel=1},Path=Name}"/>

绑定的类型AncestorType={x:Type Grid}

AncestorType指的是要找的目标对象的类型

AncestorLevel 

值得注意的是AncestorLevel需要参考AncestorType使用,如上面设置了AncestorType={x:Type Grid},则Bingding在寻找时会忽略非Grid的控件,此时g2的偏移量是1,g1的偏移量是2,DockPanel被忽略。

有时候我们不确定作为数据源的对象叫什么名字,但知道作为绑定源与UI布局有相对的关系,如下是一段XAML代码,说明多层布局控件中放置一个文本控件,来显示父级控件的名称。

<Grid x:Name="g1" Background="Red" Margin="10">
        <DockPanel x:Name="d1" Background="Orange" Margin="10">
            <Grid x:Name="g2" Background="Yellow" Margin="10">
                <DockPanel x:Name="d2" Background="LawnGreen" Margin="10">
                    <TextBox x:Name="textBox1" FontSize="24" Margin="10"/>
                </DockPanel>
            </Grid>
        </DockPanel>
    </Grid>

Csharp代码 :
 RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor);
//设定为离自己控件最近的第二层父控件
 rs.AncestorLevel = 2;
//设定父控件为Gird类型
 rs.AncestorType = typeof(Grid);
//绑定源为Grid的名称
 Binding binding = new Binding("Name") { RelativeSource=rs};
//将绑定的源放在文本显示内容中
 this.textBox1.SetBinding(TextBox.TextProperty, binding);

3、以上后台代码等同于XAML中的
Html代码:
 <TextBox x:Name="textBox1" FontSize="24" Margin="10" Text="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Grid},AncestorLevel=2},Path=Name}"/>

二.TemplatedParent 
TemplatedParent是RelativeSource的其中一种方式,使用该方式将使源元素成为模板目标类型—即TargetType;如果该绑定是在模板中,那么它的作为范围也只限于该模板.
例:
<Style TargetType="{x:Type local:TemplatedParent}">
        <Setter Property="Background" Value="Green"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:TemplatedParent}">
                    <Grid>
                        <Ellipse>
                            <Ellipse.Fill>
                                <SolidColorBrush Color="{Binding Path=Background.Color,RelativeSource={RelativeSource TemplatedParent}}"/>
                           </Ellipse.Fill>
                        </Ellipse>
                    </Grid>
                </ControlTemplate>
           </Setter.Value>
        </Setter>
    </Style>
这样绑定的源元素就指向local:TemplatedParent这个目标类型了,所以当你修改目标类型的背景颜色时,Ellipse也将跟随它变化。

三.Self
<Window x:Class="WpfApplication1.chap5_2"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 Title="chap5_2" Height="300" Width="300">
    <Grid>
           <Slider Name="slider" 
              Margin="4" Interval="1" 
             TickFrequency="1" 
             IsSnapToTickEnabled="True"
            Minimum="0" Maximum="100"
       ToolTip="{Binding RelativeSource ={ RelativeSource Self}, Path=Value}"/>
    </Grid>
</Window>
其中Binding RelativeSource={RelativeSource Self}等价于Binding RelativeSource={x:Static RelativeSource.Self}

 

 

调音台音量条,写一个自定义控件模拟调音台音量条。

数值范围0~50显示绿色,50~85显示黄色,85~100显示红色,没有数值显示褐色

https://www.cnblogs.com/lonelyxmas/p/9941830.html

 

View Injection可以让我们对于Region中显示的视图有更精确的控制。通常可以通过调试IRegionManager.AddToRegion方法或者死IRegionManager.Regions["RegionName"].Add方法来向一个Region中添加一个视图的实例。对于SingleActiveRegion(ContentControlRegionAdapter会创建这种类型的Region),可以通过IRegion.Activate方法将一个已经添加到Region中的视图显示出来。当然也可以通过IRegion.Deactivate方法来将视图状态置为非激活或者干脆调用IRegion.Remove方法将视图移除。可以看到,因为要添加的是视图的实例,所以需要仔细地设计在什么时候使用View Injection,以免造成不必要的开销。

 

1. 数据绑定之Source

  <CustomCtrlProperty1="{BindingSource={x:Static DateTime.Now}, Path=Day}"/>

这其中的Source实际上指定了属性Property1绑定的是DateTime.Now的Day值。

Source的具体解释是:指定属性绑定的Path来自于哪个Source。

2. 数据绑定之RelativeSource

       <TextBlockWidth="{Binding RelativeSource={RelativeSource Self}, Path=Parent.ActualWidth}"/>

        这其中的RelativeSource是指定了Width属性绑定的是Self.Parent.ActualWidth(TextBlock父窗口的实际大小),表示该TextBlock的宽度与父窗口的宽度一致。

           RelativeSource有四种Mode,包括FindAncestor,Self,TemplateParent,PriviousData.

RelativeSource的具体解释是: 指定与当前控件相对位置的控件作为数据绑定的源。

3. 数据绑定之ElementName,

<StackPanel Background="Blue"><Button x:Name="refButton" Background="Orange"/><Button Background="{Binding ElementName=refButton, Path=Background}"/></StackPanel>

这其中的ElementName指定了当前Button的Background属性绑定了refButton的Background的值,也就是跟refButton的Background 的值一样。

ElementName 的具体解释是:指定某个element作为该绑定的"源"。
--------------------- 

DockPanel 初始化 DockPanel 类的新实例。

 

 

ActualHeight 获取此元素的呈现高度。 (继承自 FrameworkElement。)

标签:控件,0517,Self,绑定,Binding,RelativeSource,AncestorType
来源: https://blog.csdn.net/qq_30807313/article/details/90288240

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有