C# 如何将WPF TextBlock文本绑定到不同的属性?

C# 如何将WPF TextBlock文本绑定到不同的属性?,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,基于MVVM模式,我的模型中有以下两个属性: public string DestinationCode 及 塞纳里奥:我希望文本块显示: -目的代码 -“Zzz”如果操作模式为“休眠” -“WKU”如果您的属性DestinationCode的操作模式为“唤醒”,则绑定需要INotifyPropertychange提醒其更改。例如,这是您的课程: public class Your_Class: INotifyPropertyChanged { private string _dest

基于MVVM模式,我的模型中有以下两个属性:

public string DestinationCode

塞纳里奥:我希望文本块显示:

-目的代码

-“Zzz”如果操作模式为“休眠”


-“WKU”如果您的属性
DestinationCode
的操作模式为“唤醒”

,则绑定需要
INotifyPropertychange
提醒其更改。例如,这是您的课程:

public class Your_Class: INotifyPropertyChanged
{
    private string _destinationCode;
    public string DestinationCode
    {
        get
        {
            return _destinationCode;
        }
        set
        {
            _destinationCode = value;
            RaisePropertyChanged("DestinationCode");
        }
    }

    private OperatingMode _my_Enum;
    public OperatingMode My_Enum
    {
        get
        {
            return _my_Enum;
        }
        set
        {
            _my_Enum = value;
            RaisePropertyChanged("My_Enum");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
要绑定枚举
My_enum
,必须首先创建一个转换器以获取枚举的描述。比如:

public class EnumDescriptionConverter : IValueConverter
{
    private string GetEnumDescription(Enum enumObj)
    {
        FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());

        object[] attribArray = fieldInfo.GetCustomAttributes(false);

        if (attribArray.Length == 0)
        {
            return enumObj.ToString();
        }
        else
        {
            DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute;
            return attrib.Description;
        }
    }

    object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Enum myEnum = (Enum)value;
        string description = GetEnumDescription(myEnum);
        return description;
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return string.Empty;
    }
}
接下来,您的
文本框应使用此转换器

<TextBox Text="{Binding My_Enum, Converter={StaticResource EnumDescriptionConverter}}"/>

对于您的属性
DestinationCode
,绑定需要
INotifyPropertychange
来提醒他的更改。例如,这是您的课程:

public class Your_Class: INotifyPropertyChanged
{
    private string _destinationCode;
    public string DestinationCode
    {
        get
        {
            return _destinationCode;
        }
        set
        {
            _destinationCode = value;
            RaisePropertyChanged("DestinationCode");
        }
    }

    private OperatingMode _my_Enum;
    public OperatingMode My_Enum
    {
        get
        {
            return _my_Enum;
        }
        set
        {
            _my_Enum = value;
            RaisePropertyChanged("My_Enum");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
要绑定枚举
My_enum
,必须首先创建一个转换器以获取枚举的描述。比如:

public class EnumDescriptionConverter : IValueConverter
{
    private string GetEnumDescription(Enum enumObj)
    {
        FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());

        object[] attribArray = fieldInfo.GetCustomAttributes(false);

        if (attribArray.Length == 0)
        {
            return enumObj.ToString();
        }
        else
        {
            DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute;
            return attrib.Description;
        }
    }

    object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Enum myEnum = (Enum)value;
        string description = GetEnumDescription(myEnum);
        return description;
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return string.Empty;
    }
}
接下来,您的
文本框应使用此转换器

<TextBox Text="{Binding My_Enum, Converter={StaticResource EnumDescriptionConverter}}"/>

假设文本应显示DestinationCode,除非操作模式设置为具有说明的值:

解决方案是使用多绑定和多值转换器

例如:

<Window x:Class="WpfApp3.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:local="clr-namespace:WpfApp3"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Width="200"
        Height="150"
        mc:Ignorable="d">
    <Window.Resources>
        <local:DestinationAndOperationModeToDescriptionConverter x:Key="DestinationAndOperationModeToDescription" />
    </Window.Resources>
    <Grid>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource DestinationAndOperationModeToDescription}">
                    <Binding Path="DestinationCode" />
                    <Binding Path="OperatingMode" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </Grid>
</Window>

假设文本应显示DestinationCode,除非OperationMode设置为具有说明的值:

解决方案是使用多绑定和多值转换器

例如:

<Window x:Class="WpfApp3.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:local="clr-namespace:WpfApp3"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Width="200"
        Height="150"
        mc:Ignorable="d">
    <Window.Resources>
        <local:DestinationAndOperationModeToDescriptionConverter x:Key="DestinationAndOperationModeToDescription" />
    </Window.Resources>
    <Grid>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource DestinationAndOperationModeToDescription}">
                    <Binding Path="DestinationCode" />
                    <Binding Path="OperatingMode" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </Grid>
</Window>

如果将
文本框
绑定到
DestinationCode
,则逻辑应该在
DestinationCode
中,并带有一些绑定和
INotifyPropertychange
@Bob如何在
DestinationCode>中执行此逻辑?如果您将
文本框
绑定到
目的代码
,逻辑应该在
目的代码
中,并带有一些绑定和
INotifyPropertychange
@Bob,我如何在
目的代码
中执行此逻辑?我可以有更多的解释吗?您应该为GetEnumDescription和IValueConverter.Convert管理空值。@maulikkansara我应该在哪里检查空值?枚举不可为null“对象值”可以为null或空,因为它是从字符串属性传递的。在OP的问题中,他没有在类中将其属性重新设置为字符串或枚举。所以,在我的回答中,我想这是Your_类中的一个枚举。因此,它不能为null。您正在使用“My_Enum”,但正如前面提到的,这家伙正在使用字符串属性“DestinationCode”。您应该管理GetEnumDescription和IValueConverter.Convert的null值。@Maulikansra我应该在哪里检查null?枚举不可为null“对象值”可以为null或空,因为它是从字符串属性传递的。在OP的问题中,他没有在类中将其属性重新设置为字符串或枚举。所以,在我的回答中,我想这是Your_类中的一个枚举。所以,它不能为null。您使用的是“My_Enum”,但正如前面提到的,这家伙使用的是字符串属性“DestinationCode”。