2016-10-01 47 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>