簡體   English   中英

輸入值(XAML,C#,IValueConverter)后的按鈕可見性

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

我有xaml文件,在其中放置了2個TextBox,可以在其中輸入登錄名和密碼。 之后,“登錄”按鈕應可見。 這是我的代碼:

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

當我啟動應用程序按鈕時,按鈕始終可見,我不知道出了什么問題。

為此,您需要一個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;
    }
}  

輸出值

僅用戶名

按鈕啟用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM