2016-10-26 86 views
1

我正在嘗試爲WPF綁定創建一個模板,以便在將來需要使用綁定時可以從其中提取代碼。目前綁定是不行的。爲什麼我的WPF綁定實現不能雙向工作?

我希望這段代碼能夠在用戶界面的文本框中顯示「MyString」和「MyInt」,並在用戶更改該值(因此是檢查按鈕)時正確更改邏輯。

但是,MyString和MyInt不會顯示在文本框中,也不會更改它們實際上會更改變量的值。

MainWindow.xaml.cs:

using System.Windows; 
namespace WpfBindingTemplate 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = this; 
      myController = new Controller(); 
     } 

     Controller myController; 

     private void check_string(object sender, RoutedEventArgs e) 
     { 
      MessageBox.Show("MyString: " + myController.MyString); 
     } 

     private void check_int(object sender, RoutedEventArgs e) 
     { 
      MessageBox.Show("MyInt: " + myController.MyInt); 
     } 
    } 
} 

MainWindow.xaml:

<Window x:Class="WpfBindingTemplate.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:WpfBindingTemplate" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <TextBox Name="myTextBox" Text="{Binding Path=myController.MyString}" HorizontalAlignment="Left" Height="23" Margin="202,136,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 
     <Label x:Name="label" Content="String binding:" HorizontalAlignment="Left" Margin="77,133,0,0" VerticalAlignment="Top" Width="104"/> 
     <Button x:Name="button" Content="string check" Click="check_string" HorizontalAlignment="Left" Margin="370,136,0,0" VerticalAlignment="Top" Width="75"/> 
     <Label x:Name="label1" Content="int binding:" HorizontalAlignment="Left" Margin="77,188,0,0" VerticalAlignment="Top" Width="94"/> 
     <TextBox x:Name="textBox" Text="{Binding Path=myController.MyInt}" HorizontalAlignment="Left" Height="23" Margin="202,190,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 
     <Button x:Name="button1" Click="check_int" Content="int check" HorizontalAlignment="Left" Margin="370,188,0,0" VerticalAlignment="Top" Width="75"/> 
    </Grid> 
</Window> 

控制器類:

using System.Collections.Generic; 
using System.ComponentModel; 
using System.Runtime.CompilerServices; 

namespace WpfBindingTemplate 
{ 
    public class Controller : INotifyPropertyChanged 
    { 
     public Controller() 
     { 
      MyString = "test string"; 
      MyInt = 1; 
     } 

     public int MyInt { get; set; } 

     private string myString; 
     public string MyString { get { return myString; } set { SetProperty(ref myString, value); } } 


     public event PropertyChangedEventHandler PropertyChanged; 
     private void SetProperty<T>(ref T field, T value, [CallerMemberName] string name = "") 
     { 
      if (!EqualityComparer<T>.Default.Equals(field, value)) 
      { 
       field = value; 
       var handler = PropertyChanged; 
       if (handler != null) 
       { 
        handler(this, new PropertyChangedEventArgs(name)); 
       } 
      } 
     } 
    } 
} 

回答

3

對於初學者來說,綁定不反對下地幹活,你需要在「Controller myController」上獲取/設置;並且還需要公開(在某些情況下,它也會與內部一起工作)。