首页 > 其他分享 >WPF 模型绑定-Binding

WPF 模型绑定-Binding

时间:2023-03-17 16:34:35浏览次数:32  
标签:绑定 Binding private public WPF Label MainWindow model

在 WPF 开发中会经常用到Binding,而绑定的数据源是变化的,有时候甚至数据源的结构也是变化的,View层设计多种模式,根据数据结构的变化呈现的内容和方式也会不同。下面演示一个小Demo,主要梳理一下动态改变Binding 的思路

  • 模型
public class MyModel
{
     public string Label { get; set; }
     public string Value { get; set; }
}
  • View
  <Window x:Class="WpfApp12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp12"
        mc:Ignorable="d"
        DataContext="{StaticResource model}"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions >
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBlock x:Name="tb" Text="{Binding Value}" Foreground="Red" FontSize="22" HorizontalAlignment="Center" VerticalAlignment="Center" />
       
        <StackPanel Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
            <StackPanel.Resources>
                <Style TargetType="Button">
                    <Setter Property="Width" Value="100" />
                    <Setter Property="Height" Value="35" />
                    <EventSetter Event="Click" Handler="Button_Click" />
                </Style>
            </StackPanel.Resources>
            <Button Tag="btn1" Content="测试1" />
            <Button Tag="btn2" Content="测试2" />
        </StackPanel>
    </Grid>
</Window>
  • Code
  public partial class MainWindow : Window
    {
        private Binding b1 = new Binding("Label");  //这里的Binding 对象和Path 路径是固定不变的
        private Binding b2 = new Binding("Value");
        private MyModel model;
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
            model = FindResource("model") as MyModel;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            b1.Source = model;
            b2.Source = model;
            model.Label = "This Is Label";
            model.Value = "My Is Value";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
                       
            string str = (sender as Button).Tag as string;
            if (str == "btn1") {
                
                tb.SetBinding(TextBlock.TextProperty, b1);  //这里绑定对象动态变化时

            }
            else if (str == "btn2")
            {
              
                tb.SetBinding(TextBlock.TextProperty, b2);
            }
            
        }
    }
  • 配置文件
  <Application x:Class="WpfApp12.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp12"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
          <local:MyModel x:Key="model" Value="值示例" Label="标签示例" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

标签:绑定,Binding,private,public,WPF,Label,MainWindow,model
From: https://www.cnblogs.com/sundh1981/p/17227230.html

相关文章

  • Vue——el-option下拉框绑定
    Vue——el-option下拉框绑定https://blog.csdn.net/wx19900503/article/details/1092684801、正常使用v-for进行遍历下拉框内容,如果需要增加一个自定义的值,则加一个el......
  • WPF 低代码设计参考
    WPFTreeView拖动排序拖拽排列-CSDN博客WPF:控件带阴影地任意拖动【转】-LesterDuo-博客园(cnblogs.com)WPF拖动总结-DebugLZQ-博客园(cnblogs.com)......
  • wpf自定义控件库(二)——伪3D按钮
    1、以学习wpf为目的,同时也为了增加控件代码的复用性,开始建立自己的自定义控件库;2、目前主要是根据项目需求去增加,完善控件库。希望之后能一步步扩展更多更丰富的控件;3、......
  • wpf 自定义控件库(一)
    1、以学习wpf为目的,同时也为了增加控件代码的复用性,开始建立自己的自定义控件库;2、目前主要是根据项目需求去增加,完善控件库。希望之后能一步步扩展更多更丰富的控件;3、......
  • ubuntu20.04配置双网卡绑定
    一、先设置root密码,然后切换用户ubuntu用户下:#sudopasswdroot            //设置root密码#suroot                        ......
  • Vue.js 绑定class样式
    视频<!DOCTYPEhtml><html> <head> <metacharset="UTF-8"/> <title>绑定样式</title> <style> .basic{ width:400px; height:100px; border:......
  • LiveChart for wpf
    1.引用LiveChart.Wpf的类库xmlns:lvc:="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"以直方图、折线图为例,都属于CartesianChart下的一种Series类型,例如折......
  • Vue模板语法 && 数据绑定
    模板语法Vue模板语法包括两大类插值语法功能:用于解析标签体内容。写法:{{xxx}},xxx是js表达式,可以直接读取倒data中所有区域。指令语法功能:用于解析标签(包括:标签属性......
  • WPF Progress 样式
    一、前言滚动条一般用于加载进度,我们在看视频的时候或者在浏览网页的时候经常能看到加载进度的页面。在程序开发中,默认的进度加载样式可能跟程序风格不太一样,或者加载进度......
  • WPF MenuItem 样式
    一、前言默认的MenuItem样式比较普通,这次自定义MenuItem的样式也只是对MenuItem的颜色风格进行变化。需要其他功能的变化,大家可以根据样式代码进行扩展。MenuItem的样式......