WPF之元素绑定

元素绑定——WPF从源对象提取一些信息,并用这些对象设置目标对象的属性。

绑定的模式:

OneWay  

当源属性变化时更新目标属性
TwoWay  当源属性变化时更新目标属性,并且当目标属性变化时同时更新源属性
OneTime 最初根据源属性的值设置目标属性
OneWayToSource 与OneWay类型相似,但方向相反
Default 依赖于目标属性

 

 

 

 

 

窗体如下:

 

 

在xmal文件中进行绑定

  <StackPanel>
        <Slider Name="slider1" Minimum="0" Maximum="40" TickFrequency="1" Value="20" TickPlacement="TopLeft"></Slider>
        <TextBlock Name="textBlock" Text="Lynn Test"
                  FontSize="{Binding Source=slider1,Path=Value,Mode=TwoWay}"  
                   />
        <Button Name="button1" Content="Change TextBlock FontSize" Click="button1_Click" Height="50"/>
        <Button Name="button2" Content="" Height="45"/>
    </StackPanel>

在代码中进行绑定

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Binding bing = new Binding();
            bing.Source = this.slider1;
            bing.Path = new PropertyPath("Value");
            bing.Mode = BindingMode.TwoWay;

            this.textBlock.SetBinding(TextBlock.FontSizeProperty, bing);
        }

  

posted @ 2020-04-05 15:11  帝雪寒天  阅读(172)  评论(0编辑  收藏  举报