WPF常用五种数据绑定方式
绑定方式一(绑定元素依赖属性)
<StackPanel> <Slider Name="s1" Value="10" Maximum="100"></Slider> <TextBlock FontSize="{Binding ElementName=s1,Path=Value}" Text="看着我" ></TextBlock> </StackPanel>
绑定方式二(绑定RelativeSources)
第一种关系: Self
<TextBlock FontSize="18" FontWeight="Bold" Margin="10" Background="Red" Width="80" Height="{Binding RelativeSource={RelativeSource Self},Path=Width}">MultiBinding Sample</TextBlock>
第二种关系:TemplatedParent
<Style TargetType="{x:Type Button}"><Setter Property="Background" Value="Green"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type Button}"><Grid><Ellipse><Ellipse.Fill><SolidColorBrush Color="{Binding Path=Background.Color,RelativeSource={RelativeSource TemplatedParent}}"/> </Style>
第三种关系:AncestorType
<Grid><Label Background = {Binding Path=Background, RelativeSource={RelativeSource FindAncestor AncestorType={x:Type Grid}}}/> </Grid>
绑定方式三(StaticResource静态资源/DynamicResource动态资源)
<Window.Resources> <sys:String x:Key="Content" > Hello World! </sys:String> </Window.Resources> <Grid> <WrapPanel> <TextBlock Text="静态"/> <TextBox Text="{StaticResource Content}" Width="100" x:Name="TextBox1"/> <TextBlock Text="动态" Margin="10,0,0,0"/> <TextBox Text="{DynamicResource Content}" Width="100" x:Name="TextBox2"/> <Button Content="改变资源值" Click="ChangeBtn_Click" Width="100"/> </WrapPanel> </Grid>
CS代码
private void ChangeBtn_Click(object sender, RoutedEventArgs e) { this.Resources["Content"] = "内容变了"; }
不管是动态资源还是静态资源,都需要现在资源里定义好"X:Name"资源扩展标记"资源字典"比如<sys:String x:Key="Content" > Hello World! </sys:String>
绑定方式四(绑定到集合元素 ItemSource)
XAML代码
<Border Grid.Row="2" Grid.Column="3"> <ListBox x:Name="todoList" HorizontalContentAlignment="Stretch" ItemsSource="{Binding VarList}" ScrollViewer.VerticalScrollBarVisibility="Hidden"> <ListBox.ItemTemplate> <DataTemplate> <DockPanel MaxHeight="80" LastChildFill="False"> <ToggleButton DockPanel.Dock="Right" /> <StackPanel> <TextBlock FontSize="16" FontWeight="Bold" Text="{Binding Name}" /> <TextBlock Margin="0,5" Opacity="0.5" Text="{Binding Value}" /> <TextBlock Margin="0,5" Opacity="0.5" Text="{Binding Description}" /> <TextBlock Margin="0,5" Opacity="0.5" Text="{Binding InsertTime}" /> </StackPanel> </DockPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Border>
CS代码
public ReportDataViewModel() { VarList.Add(new ActualData() { Description = "浮点数1", Name = "Float1", InsertTime = DateTime.Now, Value = CommonMethods.CurrentPLCValue["Float1"] }); VarList.Add(new ActualData() { Description = "浮点数2", Name = "Float2", InsertTime = DateTime.Now, Value = CommonMethods.CurrentPLCValue["Float2"] }); VarList.Add(new ActualData() { Description = "浮点数3", Name = "Float3", InsertTime = DateTime.Now, Value = CommonMethods.CurrentPLCValue["Float3"] }); VarList.Add(new ActualData() { Description = "浮点数4", Name = "Float4", InsertTime = DateTime.Now, Value = CommonMethods.CurrentPLCValue["Float4"] }); } private ObservableCollection<ActualData> varList = new ObservableCollection<ActualData>(); public ObservableCollection<ActualData> VarList { get { return varList; } set { varList = value; } }
绑定方式五(DataContext)(最常规的绑定)
XAML代码
1 <Window x:Class="MyWPFSimple5.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:MyWPFSimple5" 7 mc:Ignorable="d" DataContext="{Binding Source={StaticResource vmLocator}, Path=HelloObj}" 8 Title="MainWindow" Height="250" Width="400"> 9 <Grid> 10 <StackPanel> 11 <TextBox Text="{Binding Say}" BorderBrush="Black" Margin="10"/> 12 <Button Content="改变内容" Click="Button_Click" Margin="10"/> 13 </StackPanel> 14 </Grid> 15 </Window>
CS代码
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using System.Windows; 8 using System.Windows.Controls; 9 using System.Windows.Data; 10 using System.Windows.Documents; 11 using System.Windows.Input; 12 using System.Windows.Media; 13 using System.Windows.Media.Imaging; 14 using System.Windows.Navigation; 15 using System.Windows.Shapes; 16 17 namespace MyWPFSimple5 18 { 19 /// <summary> 20 /// MainWindow.xaml 的交互逻辑 21 /// </summary> 22 public partial class MainWindow : Window 23 { 24 public MainWindow() 25 { 26 InitializeComponent(); 27 } 28 29 private void Button_Click(object sender, RoutedEventArgs e) 30 { 31 Hello hello = DataContext as Hello; 32 hello.Say += "+1"; 33 } 34 } 35 36 public class Locator 37 { 38 public Locator() 39 { 40 //HelloObj = new Hello(); 41 } 42 43 private Hello m_HelloObj; 44 public Hello HelloObj 45 { 46 get 47 { 48 if (m_HelloObj == null) 49 { 50 m_HelloObj = new Hello(); 51 } 52 return m_HelloObj; 53 } 54 set 55 { 56 m_HelloObj = value; 57 } 58 } 59 } 60 61 public class Hello : INotifyPropertyChanged 62 { 63 public Hello() 64 { 65 Say = "Hello World!"; 66 } 67 private string m_Say; 68 public string Say { get { return m_Say; } set { m_Say = value; RaisedPropertyChanged(nameof(Say)); } } 69 70 public event PropertyChangedEventHandler PropertyChanged; 71 protected virtual void RaisedPropertyChanged(string name) 72 { 73 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); 74 } 75 } 76 }
标签:Windows,绑定,System,Hello,Basic,using,WPF,public From: https://www.cnblogs.com/HomeSapiens/p/17417443.html