UWP,<Button.IsEnabled><Binding.Source><Binding.Converter>

问题描述 投票:0回答:1

在 UWP 项目中,我无法像 WPF 中那样访问 IMultiValueConverter。 然后我尝试在 UWP 中实现类似的东西。 这是我正在做的方式:

//------------------------------------------------

public class Product : ViewModelBase
{
private string _Name;
public string Name
{
    get { return _Name; }
    set { this.SetProperty(ref this._Name, value); }
}

private string _Description;
public string Description
{
    get { return _Description; }
    set { this.SetProperty(ref this._Description, value); }
}

//--------------------------------------

public class FurnitureMultiConditionToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
    if (value is Furniture data)
    {
        // Check if all conditions are met
        if (string.IsNullOrEmpty(data.Name) 
            && !string.IsNullOrEmpty(data.Description)
            )
        {
            return true; // Return true if all conditions pass
        }
    }
    return false; // Return false if any condition fails
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
    throw new NotImplementedException();
}

}

//------------------------------------

                <TextBox x:Name="tbFurnitureName" x:Uid="Name" Grid.Row="0" Text="tewstname" TextChanged="tbFurnitureName_TextChanged"></TextBox>
                <TextBox x:Name="tbFurnitureDescription" x:Uid="Description" Grid.Row="1" Text="test desc" TextChanged="tbFurnitureDescription_TextChanged"></TextBox>
                <Button Grid.Row="2" Name="bSaveFurniture" x:Uid="Save" Click="bSaveFurniture_Click">
                    <Button.IsEnabled>
                        <Binding Path="">
                            <Binding.Source>
                                <modelsMP:Furniture
                                    Name="{Binding Text, ElementName=tbFurnitureName, FallbackValue=''}"
                                    Description="{Binding Text, ElementName=tbFurnitureDescription, FallbackValue=''}"
                                />
                            </Binding.Source>
                            <Binding.Converter>
                                <convertersMP:FurnitureMultiConditionToBooleanConverter />
                            </Binding.Converter>
                        </Binding>
                    </Button.IsEnabled>
                </Button>

//------------------------------

这两行导致了问题:

Name="{Binding Text, ElementName=tbFurnitureName, FallbackValue=''}"
Description="{Binding Text, ElementName=tbFurnitureDescription, FallbackValue=''}"

这是我收到的错误消息:

Windows.UI.Xaml.Markup.XamlParseException: 'The text associated with this error code could not be found.

//------------------------------

谢谢您的帮助

data-binding uwp ivalueconverter xamlparseexception
1个回答
0
投票

我不确定你的代码是否应该工作,但你应该看看 MultiBindingBehavior

另外,由于这是 UI 相关逻辑,我只是在代码隐藏中实现它。

例如:

主窗口.xaml

<Window
    x:Class="App1.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="using:App1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel>
        <TextBox
            x:Name="TextBoxControl1"
            TextChanged="TextBoxControl_TextChanged" />
        <TextBox
            x:Name="TextBoxControl2"
            TextChanged="TextBoxControl_TextChanged" />
        <Button
            x:Name="ButtonControl"
            Click="ButtonControl_Click"
            Content="Click" />
    </StackPanel>
</Window>

MainWindow.xaml.cs

public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
        this.RefreshIsButtonEnabled();
    }

    private void RefreshIsButtonEnabled()
    {
        this.ButtonControl.IsEnabled =
            string.IsNullOrEmpty(this.TextBoxControl1.Text) is false &&
            string.IsNullOrEmpty(this.TextBoxControl2.Text) is false;
    }

    private void TextBoxControl_TextChanged(object sender, TextChangedEventArgs e)
    {
        this.RefreshIsButtonEnabled();
    }

    private void ButtonControl_Click(object sender, RoutedEventArgs e)
    {
        this.ButtonControl.Content = "Clicked";
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.