2017-05-25 52 views
0

我有xaml文件,我把2個文本框放在哪裏我可以把登錄名和密碼。之後,按鈕「登錄」應該是可見的。這裏是我的代碼:輸入值後的按鈕可見性(XAML,C#,IValueConverter)

TreeView.xaml文件:

<UserControl x:Class="LayoutMVVM.Views.TreeView" 
    xmlns:local="clr-namespace:LayoutMVVM.Views" 
    xmlns:Treemodels="clr-namespace:LayoutMVVM.ViewModels" .../> 
    <UserControl.Resources> 
     <Treemodels:VBConverter x:Key="VBConverter" /> 
    </UserControl.Resources> 
<Grid> 
.... 
    <TextBox Grid.Row="2" Grid.Column="2" Text="{Binding Login, UpdateSourceTrigger=PropertyChanged}" Background="AliceBlue" /> 
    <TextBox Grid.Row="1" Grid.Column="2" Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}" Background="AliceBlue" /> 

    <Button Grid.Row="3" Grid.Column="3" Visibility="{Binding IsVerifyTrue, Converter={StaticResource VBConverter}}" Content="Login" /> 
</Grid> 

TreeView.xaml.cs

namespace LayoutMVVM.Views 
{ 
public partial class TreeView : UserControl 
{ 
    public TreeView() 
    { 
    InitializeComponent(); 
    } 
    public TreeView _isVerifyTrue; 
    public TreeView IsVerifyTrue 
    { 
     get { return this; } 
     set 
     { 
      _isVerifyTrue = value; 
      OnPropertyChanged(() => IsVerifyTrue); 
     } 
    } 

    private void OnPropertyChanged(Func<object> p) 
    { 
     throw new NotImplementedException(); 
    } 

    private string _login; 
    public string Login 
    { 
     get { return _login; } 
     set 
     { 
      _login = value; 
      IsVerifyTrue = this; 
      OnPropertyChanged(() => Login); 
     } 
    } 

    private string _password; 
    public string Password 
    { 
     get { return _password; } 
     set 
     { 
      _password = value; 
      IsVerifyTrue = this; 
      OnPropertyChanged(() => Password); 
     } 
    } 
} 

和獨立類VBConverter.cs

namespace LayoutMVVM.ViewModels 
{ 
    public class VBConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      if (value is TreeView) 
      { 
       var tv = value as TreeView; 
       bool result = !string.IsNullOrEmpty(tv.Login) 
        && !string.IsNullOrEmpty(tv.Password); 
       return result? Visibility.Visible : Visibility.Hidden; 
      } 
      return Visibility.Hidden; 
     } 

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

當我啓動應用程序按鈕時總是可見的,我不知道什麼是錯的。

回答

1

對此,您需要一個MultiValueConverter。見下面的例子:

XAML:

<Window x:Class="SOSample1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:SOSample1" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <Grid.Resources> 
     <local:VisibilityConverter x:Key="VisibilityConverter" /> 
    </Grid.Resources> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="100" /> 
     <RowDefinition Height="100" /> 
     <RowDefinition Height="100" /> 
    </Grid.RowDefinitions> 

    <TextBox Grid.Row="0" Name="user" Height="100" Width="200" Text="{Binding Login, UpdateSourceTrigger=PropertyChanged}" Background="AliceBlue" /> 
    <TextBox Grid.Row="1" Name="password" Height="100" Width="200" Text="{Binding Login, UpdateSourceTrigger=PropertyChanged}" Background="AliceBlue" /> 
    <Button Grid.Row="2" Content="Login" Height="100" Width="200" > 
     <Button.Visibility> 
      <MultiBinding Converter="{StaticResource VisibilityConverter}" > 
       <Binding ElementName="user" Path="Text" /> 
       <Binding ElementName="password" Path="Text" /> 
      </MultiBinding> 
     </Button.Visibility> 
    </Button> 
</Grid> 

轉換器:

public class VisibilityConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    {    
     bool result = false; 
     if (values != null) 
     { 
      foreach (var item in values) 
      { 
       result = (item as string).Length > 0; 
       if (!result) break; 
      }     
     } 
     return (Visibility)new BooleanToVisibilityConverter().Convert(result, targetType, parameter, culture); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    {    
     return null; 
    } 
} 

OUTPUT

Only username

Button enables

+0

許多感謝@Kylo!它的作品完美.....一個QQ:如果它是一個TextBox,那麼IMultiValueConverter或IValueConverter? – 4est

+0

@ 4est僅適用於單個文本框IValueConverter是必需的。 –

+0

再次感謝你! – 4est