C# 在windows Phone中使用依赖项属性启用/禁用控件
我想禁用我的登录按钮,直到用户在windows phone的用户名和密码字段中输入至少一个字符。我在转换器中定义了两个依赖属性并检查它们的值,但所有这些都不是我的转换器代码C# 在windows Phone中使用依赖项属性启用/禁用控件,c#,windows-phone-8,C#,Windows Phone 8,我想禁用我的登录按钮,直到用户在windows phone的用户名和密码字段中输入至少一个字符。我在转换器中定义了两个依赖属性并检查它们的值,但所有这些都不是我的转换器代码 public class LoginEnableConverter : DependencyObject,IValueConverter { public static DependencyProperty dep_username = DependencyProperty.Register("Dep_UserNam
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>
在按钮的控件绑定中,实际上没有绑定到值。值转换器代码未运行,因为没有任何提示它运行。相反,您需要绑定到一个属性值,并在任一框中键入字符时更改该值,例如 另外,查看依赖项属性时,实际上没有调用setter的地方。真正需要这样做的地方是在您用来绑定到用户名和密码字段的任何对象中 在任何情况下,这都不会让人感觉到数据绑定是实现最终结果的合适方法。相反,您需要做的是绑定到文本框上的
TextChanged
事件,并将启用/禁用逻辑移动到事件处理程序的代码中。为此,请将以下属性添加到每个文本框中:TextChanged=“OnUserOrPasswordTextChange”
然后,在页面的代码隐藏中
private void OnUserOrPasswordTextChange(object sender, TextChangedEventArgs e)
{
btnLogin.IsEnabled = !string.IsNullOrEmpty(txtUsername.Text) && !string.IsNullOrEmpty(Password.Text);
}
请注意,我没有测试,但您应该了解我的建议
有关数据绑定的更多阅读,请参阅关于数据绑定以了解这些概念。请密切关注他们在代码隐藏中所做的工作。Hi@rmayer06您是否提供了任何示例我看不出这对我更有帮助,因为我对所有这些都是新手。这实际上比仅提供示例要复杂一些。我会看看是否能给你推荐一篇好文章。你知道,想想看,你最好的办法是连接到文本框上的
TextChanged
事件,并以这种方式处理逻辑。我将编辑我的答案。是的,我可以这样做,但我必须使用绑定实现这一点。非常感谢您的帮助。您要求使用绑定的理由是什么?请注意,用户有使用MVVM的要求(根据下面的讨论),不允许直接连接到事件。