2016-10-01 42 views
0

我目前的工作如何使用XAML以及它如何與C#交互。我目前面臨的挑戰是試圖獲取文本塊來更改顯示覆選框時顯示的文本。這需要程序取得一個Bool輸入(是否勾選了方框?)並給出一個字符串輸出。當綁定我怎麼數據類型之間的轉換?

目前,當它運行的佈局是正確的讓我懷疑XAML代碼是好的但是複選框是否被選中與否的文本塊將只顯示「取消選中」狀態。

我懷疑,問題是這兩種方法之間,但我無法找到一個解決方案,有什麼建議?

有問題的代碼:C#

public class MainPageViewModel : ViewModelBase 
{ 
    //stores value of checkbox 
    private bool _BoxCheckBool; 

    //Updates value of _BoxCheckBool 
    public bool BoxCheckBool 
    { 
     set 
     { 
      Set(ref _BoxCheckBool, value); 
     }   
    } 

    //stores value (for textblock) 
    private string _BoxCheckString; 

    public string BoxCheckString 
    { 
     //logic that determines what will be sent to the textblock 
     get 
     { 
      if (_BoxCheckBool == true) 
      { 
       _BoxCheckString = "The Box has been checked"; 
      } 

      else if (_BoxCheckBool == false) 
      { 
       _BoxCheckString = "The Box has not been checked"; 
      } 

      else 
      { 
       _BoxCheckString = "ERROR"; 
      } 

      return _BoxCheckString; 
     } 

     set 
     { 
      Set(ref _BoxCheckString, value); 
     } 
    } 
} 

有問題的代碼:XAML

<CheckBox x:Name="BoxTest" HorizontalAlignment="Center" Content="Check Box" IsChecked="{Binding BoxCheckBool, Mode=TwoWay}"/> 

    <TextBlock x:Name="BoxTestOutput" Grid.Row="1" Text="{Binding BoxCheckString, Mode=TwoWay}"/> 
+0

謝謝大家對你的建議,它一直是非常有幫助的。 :) – TheSkeletonDetective

回答

0

,您只需要在bool屬性更改募集PropertyChanged事件。 UI將自動更新

public class MainPageViewModel : ViewModelBase 
{ 
    //stores value of checkbox 
    private bool _BoxCheckBool; 

    //Updates value of _BoxCheckBool 
    public bool BoxCheckBool 
    { 
     set 
     { 
      Set(ref _BoxCheckBool, value); 
      // Assumes your ViewModelBase have method to raising this 
      RaisePropertyChanged("BoxCheckString"); 
     }   
    } 

    private string _BoxCheckString; 

    public string BoxCheckString 
    { 
     //logic that determines what will be sent to the textblock 
     get 
     { 
      if (_BoxCheckBool == true) 
      { 
       _BoxCheckString = "The Box has been checked"; 
      } 
      else 
      { 
       _BoxCheckString = "The Box has not been checked"; 
      } 
      // Boolean type have only two value (true/false) 
      // - you don't need checking for "errors" 
      return _BoxCheckString; 
     } 
     set 
     { 
      Set(ref _BoxCheckString, value); 
     } 
    } 
} 
0

文本塊未更新,因爲當值更改時沒有通知。

要進行綁定更新,需要實現property change notification (Please check out this link)。您需要在BoxCheckBool的設置程序中調用類似OnPropertyChanged("BoxCheckString")(取決於您的ViewModelBase),以便文本塊知道需要更新。

+0

啊,好的。出於興趣,我需要還要告訴第二種方法檢查更新或不正文塊自動引用它,因爲它這樣做? – TheSkeletonDetective

+0

只需確保在BoxCheckString更新爲所需值後爲'BoxCheckString'引發'PropertyChanged'事件,以便文本塊可以獲得最新的'BoxCheckString'。文本塊將自動更新爲「BoxCheckString」的每個「PropertyChanged」事件。 –

0

你正在改變文本值是放錯了地方:

視圖模型:

private bool _BoxCheckBool; 

    //Updates value of _BoxCheckBool 
    public bool BoxCheckBool 
    { 
     get 
     { 
      return _BoxCheckBool; 
     } 
     set 
     { 
      _BoxCheckBool = value; 
      OnPropertyChanged("BoxCheckBool"); 
      if (_BoxCheckBool == true) 
      { 
       BoxCheckString = "The Box has been checked"; 
      } 

      else if (_BoxCheckBool == false) 
      { 
       BoxCheckString = "The Box has not been checked"; 
      } 

      else 
      { 
       BoxCheckString = "ERROR"; 
      } 
     } 
    } 

    //stores value (for textblock) 
    private string _BoxCheckString = ""; 

    public string BoxCheckString 
    { 
     //logic that determines what will be sent to the textblock 
     get 
     { 
      return _BoxCheckString; 
     } 
     set 
     { 
      _BoxCheckString = value; 
      OnPropertyChanged("BoxCheckString");  
     } 
    } 

的XAML:

<StackPanel Orientation="Horizontal"> 
     <CheckBox Name="BoxTest" IsChecked="{Binding BoxCheckBool, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
     <ContentPresenter Content="{Binding BoxCheckString, UpdateSourceTrigger=PropertyChanged}" /> 
    </StackPanel>