何时绑定如何在数据类型之间进行转换?

时间:2016-10-01 10:46:05

标签: c# xaml data-binding

我目前正在研究如何使用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}"/>

3 个答案:

答案 0 :(得分:0)

PropertyChanged属性发生更改时,您只需要引发bool事件。用户界面将自动更新

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);
        }
    }
}

答案 1 :(得分:0)

文本块未更新,因为更改值时没有通知。

要进行绑定更新,您需要实施property change notification (Please check out this link)。您需要在OnPropertyChanged("BoxCheckString")的设置器中调用类似ViewModelBase(取决于您的BoxCheckBool)的内容,以便文本块知道需要更新。

答案 2 :(得分: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>