【问题标题】:How to bind a Button's IsEnabled member to a TextBox's content?如何将 Button 的 IsEnabled 成员绑定到 TextBox 的内容?
【发布时间】:2016-10-04 16:21:59
【问题描述】:

我有一个相当标准的设置:根据TextBox 的内容是否为空,我想启用/禁用Button 控件。

我试过了,什么在 WPF 应用程序中可以工作,但是在 Windows 运行时运行时不会产生所需的行为:

<StackPanel>
    <TextBox x:Name="ItemNameTextBox" Header="Item" />
    <Button Content="Add Item"
            IsEnabled="{Binding ElementName=ItemNameTextBox,
                                Path=Text.Length,
                                Mode=OneWay}" />
</StackPanel>

但是,在运行时,这会产生以下调试输出1)

Error: BindingExpression path error: 'Length' property not found on 'Windows.Foundation.IReference`1<String>'.

错误消息是正确的:Windows::Foundation::IReference&lt;String&gt; 没有Length 属性。

问题:如何将ButtonIsEnabled 属性绑定到TextBoxs 文本长度属性?我必须实现自定义数据转换器,还是只能使用标记来完成?


1)完整的错误输出:
Error: BindingExpression path error: 'Length' property not found on 'Windows.Foundation.IReference`1<String>'. BindingExpression: Path='Text.Length' DataItem='Windows.UI.Xaml.Controls.TextBox'; target element is 'Windows.UI.Xaml.Controls.Button' (Name='null'); target property is 'IsEnabled' (type 'Boolean')

【问题讨论】:

    标签: xaml windows-phone-8 windows-runtime winrt-xaml c++-cx


    【解决方案1】:

    您不能将布尔值绑定到整数。

    您需要创建一个转换器,该转换器返回一个布尔值,其值为真或假,具体取决于整数的值。在您的情况下,我假设您希望长度为零返回 false,长度大于零返回 true。如果在 c++/cx 中长度不是属性而是方法(与 C# 不同),那么您将无法直接绑定到它。您将不得不绑定 Text 属性,然后在转换器中调用 Length 方法:

    public ref class LengthToIsEnabledConverter sealed : public Windows::UI::Xaml::Data::IValueConverter
    {
    public:
        virtual Platform::Object^ Convert( Platform::Object^ value,
                                           Windows::UI::Xaml::Interop::TypeName targetType,
                                           Platform::Object^ parameter,
                                           Platform::String^ language ) {
            Platform::String^ text = safe_cast<Platform::String^>( value );
            return ( text->Length() > 0 );
        }
    
        virtual Platform::Object^ ConvertBack( Platform::Object^ value,
                                               Windows::UI::Xaml::Interop::TypeName targetType,
                                               Platform::Object^ parameter,
                                               Platform::String^ language ) {
            throw ref new Platform::NotImplementedException();
        }
    };
    

    然后在绑定中添加转换器并将路径更改为文本:

    IsEnabled="{Binding ElementName=ItemNameTextBox,
                        Path=Text,
                        Mode=OneWay,
                        Converter={StaticResource LengthToIsEnabledConverter}" />
    

    转换器在 XAML 中的声明如下:

    <UserControl.Resources>
        <converters:LengthToIsEnabledConverter x:Key="LengthToIsEnabledConverter " />
    </UserControl.Resources>
    

    【讨论】:

    • 这不太成功。正如调试诊断所说:“'Length' property not found”,所以不能使用。改为传递Text 属性,并在转换器中评估Length() 方法的返回值按预期工作。不过,我不确定所有这些是否都是必要的。 Platform::String 真的与 Windows 运行时中的数据绑定不同吗?
    • @IInspectable - 啊。错过了那个方面。我认为是 int -> bool 失败导致了该消息,而不是它首先没有找到该属性,但绑定到 TextBox 的 Text 是一个可接受的解决方案。
    • 这看起来像 C#。 string 类型是Platform::String 的投影,它是否提供Length 作为属性(而不是成员函数)?我正在使用 C++/CX,所以我不知道。
    • @IInspectable 这将教我正确阅读标签;)现在答案应该有意义了。
    猜你喜欢
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多