简体   繁体   中英

Button visibility after enter value (XAML, C#, IValueConverter)

I have xaml file where I put 2 TextBox where I can put login and password. After then Button "Login" should be visible. Here is my code:

TreeView.xaml file:

<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);
        }
    } 
}

and seperate class for 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(); 
         } 
     }
}

When I start app button is always visible and I'm not know what is wrong.

You'll need a MultiValueConverter for this. See below example :

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>

Converter :

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

仅用户名

按钮启用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM