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>