WPF 之 Binding 数据驱动UI—实战

1、一个TextBox显示另一个TextBox文本的第4个字符

            <TextBox x:Name="tb1"/>
            <!--tb2绑定:单向绑定 tb1的Text属性第4个字符-->
            <TextBox x:Name="tb2" Text="{Binding Path=Text.[3],ElementName=tb1,Mode=OneWay}"/>


2、ComboBox 绑定List

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            this.DataContext = new Window1Model();
        }
    }

    public class Window1Model
    {
        public List<string> list{get;set;}
        public Window1Model()
        { 
            list = new List<string> { "1", "2", "3" };
        }
    }


<ComboBox x:Name="a" ItemsSource="{Binding list}" SelectedIndex="0"/>


3、ComboBox 绑定 资源字典

创建资源字典 TreeFile.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:s="clr-namespace:System;assembly=mscorlib"
                    >
    <x:ArrayExtension x:Key="TermTypeList" Type="{x:Type s:String}">
        <s:String>A</s:String>
        <s:String>B</s:String>
        <s:String>C</s:String>
    </x:ArrayExtension>
</ResourceDictionary> 

App.xaml

<Application x:Class="AutomaticConfigurationAPP.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Resources/Xml/TreeFile.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

<ComboBox ItemsSource="{Binding Source={StaticResource TermTypeList}}"/>


4、ComboBox 绑定 XML

<?xml version="1.0" encoding="utf-8" ?>
<Data xmlns="">
  <Grade Name="一年级"/>
  <Grade Name="二年级"/>
</Data>


    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            this.DataContext = new Window1Model();
        }
    }

    public class Window1Model
    {
        public IEnumerable<string> list { get; set; }
        public Window1Model()
        {

            XDocument xdoc = XDocument.Load(Environment.CurrentDirectory + "\\XMLFile1.xml");
            list = from element in xdoc.Descendants("Grade")
                   select element.Attribute("Name").Value.ToString();
        }
    }


<ComboBox ItemsSource="{Binding list}"/>

5、List集合Add后,view层没发生变化。

使用动态集合 ObservableCollection

        private ObservableCollection<VariableModel> listvariable;
        public ObservableCollection<VariableModel> ListVariable
        {
            get { return listvariable; }
            set
            {
                listvariable = value;
                NotifyPropertyChanged("ListVariable");
            }
        }


6、点击鼠标左键,命令不执行,使用相对数据源,找到命令

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"


                <TextBlock x:Name="a" Text="{Binding Name}">
                              <i:Interaction.Triggers>
                                <i:EventTrigger EventName="MouseLeftButtonDown">
                                    <i:InvokeCommandAction Command="{Binding (DataContext).TreeSelectCommand,
                        RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page}}" CommandParameter="{Binding Name}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                </TextBlock>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tiz198183

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值