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

本文关键字:之间 转换 数据类型 定时 绑定 | 更新日期: 2023-09-27 18:09:43

我目前正在研究如何使用XAML以及它如何与c#交互。我目前的挑战是试图得到一个文本块,以改变它显示的文本,当一个复选框被选中。这要求程序接受Bool输入(复选框是否勾选了?)并给出字符串输出。

目前,当它运行的布局是正确的,使我怀疑XAML代码是好的,但是文本块将只显示"unticked"状态,无论复选框被选中与否。

我怀疑问题是在两种方法之间,但我找不到解决方案,有什么建议吗?

问题代码: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}"/>

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

您只需要在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);
        }
    }
}

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

要进行绑定更新,需要实现属性更改通知(请查看此链接)。您需要在BoxCheckBool的setter中调用OnPropertyChanged("BoxCheckString")(取决于您的ViewModelBase)之类的东西,以便文本块知道需要更新。

您更改文本值的位置不对:

ViewModel:

 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>