【发布时间】: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