【问题标题】:Enable/Disable a control using Dependency Property in windows Phone在 windows Phone 中使用依赖属性启用/禁用控件
【发布时间】:2014-11-24 20:47:25
【问题描述】:

我希望我的登录按钮禁用,直到用户在 windows phone 的用户名和密码字段中输入至少一个字符。我在转换器中定义了两个依赖属性并检查它们的值,但所有这些在这里不起作用的是我的转换器代码。

public class LoginEnableConverter : DependencyObject,IValueConverter
{
    public static DependencyProperty dep_username = DependencyProperty.Register("Dep_UserName", typeof(string), typeof(LoginEnableConverter), new PropertyMetadata(null));

    public string Dep_UserName
    {
        get { return (string)GetValue(dep_username); }
        set { SetValue(dep_username, value); }
    }

    public static DependencyProperty dep_password = DependencyProperty.Register("Dep_Password", typeof(string), typeof(LoginEnableConverter), new PropertyMetadata(null));

    public string Dep_Password
    {
        get { return (string)GetValue(dep_password); }
        set { SetValue(dep_password, value); }
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return !string.IsNullOrEmpty(Dep_UserName) && !string.IsNullOrEmpty(Dep_Password);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return !string.IsNullOrEmpty(Dep_UserName) && !string.IsNullOrEmpty(Dep_Password);
    }
}

将属性绑定到转换器

<converter:LoginEnableConverter x:Key="EnableLogin" Dep_Password="{Binding Path=DataContext.Password}" Dep_UserName="{Binding Path=DataContext.UserName}"></converter:LoginEnableConverter>

我的 XAML 代码

<toolkit:PhoneTextBox Grid.Column="0" Text="{Binding UserName, Mode=TwoWay}" Grid.Row="0" Margin="0,50,0,0" Background="#C9F2EE"  HorizontalAlignment="Stretch" ActionIcon="/Assets/user_ic.png" FlowDirection="LeftToRight"  Hint="Username" Name="txtUsername"   
                 BorderBrush="Gray" BorderThickness="1" TextWrapping="NoWrap" VerticalAlignment="Top" Style="{StaticResource PhoneTextBoxStyle1}">
    </toolkit:PhoneTextBox>

    <toolkit:PhoneTextBox Grid.Column="0" Text="{Binding Password, Mode=TwoWay}" Margin="0,30,0,0" Grid.Row="1" Name="Password"  Background="#C9F2EE"  ActionIcon="/Assets/key_ic.png"  Style="{StaticResource PhoneTextBoxStyle1}"   Hint="Password" 
                 BorderBrush="Gray" BorderThickness="1" VerticalAlignment="Top">            
    </toolkit:PhoneTextBox>

    <Button x:Name="btnLogin" Grid.Row="3"  Margin="0,40,0,0" Content="Log in" IsEnabled="{Binding Converter={StaticResource EnableLogin}}" HorizontalAlignment="Center" VerticalAlignment="Center"  Width="293" Foreground="White" Background="Gray" Height="81" Style="{StaticResource ButtonStyle3}">
    </Button>

【问题讨论】:

  • 请注意,用户有使用 MVVM 的要求(根据下面的讨论),并且不允许直接连接到事件。

标签: c# windows-phone-8


【解决方案1】:

在按钮的控件绑定中,您实际上并没有绑定到值。值转换器代码不会运行,因为没有任何提示它运行。相反,您需要绑定到一个属性值,并在任一框中输入字符时更改该值。

此外,查看您的依赖项属性,您实际上并没有调用 setter 的地方。真正需要发生这种情况的地方是您用来绑定到用户名和密码字段的任何对象。

无论如何,这并不让人觉得数据绑定是实现最终结果的适当方法。相反,您需要做的是绑定到文本框上的 TextChanged 事件,并将启用/禁用逻辑移动到事件处理程序的代码中。为此,请将以下属性添加到每个文本框:TextChanged="OnUserOrPasswordTextChange"

然后,在页面的代码隐藏中,

private void OnUserOrPasswordTextChange(object sender, TextChangedEventArgs e)
{
     btnLogin.IsEnabled = !string.IsNullOrEmpty(txtUsername.Text) && !string.IsNullOrEmpty(Password.Text);
}

请注意,我没有测试,但您应该明白我的建议。

有关数据绑定的更多信息,请参阅this article 关于数据绑定以了解概念。密切关注他们在代码隐藏中所做的事情。

【讨论】:

  • 嗨@rmayer06 你有没有提供任何我看不到的例子对我更有帮助,因为我对所有这些都是新手
  • 这实际上比仅仅提供一个例子要复杂一些。我看看能不能给你推荐一篇好文章。
  • 你知道,考虑一下 - 你最好的办法是连接到文本框上的 TextChanged 事件并以这种方式处理逻辑。我将编辑我的答案。
  • 是的,我可以做到,但我必须使用绑定来实现。任何帮助将不胜感激。
  • 您要求使用绑定的理由是什么?
猜你喜欢
  • 2011-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多