2017-03-14 16 views
1

私はWpf(xaml)でコンバータを使用する方法を学びます。コンバータboolToVisibility

<Window x:Class="TextExpanderGriglia.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:TextExpanderGriglia" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> 
<Window.Resources> 
    <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" /> 
</Window.Resources> 
<Grid> 
    <Button Content="CIAO" Width="50" Height="50" Visibility="{Binding vButton,Converter={StaticResource BoolToVisibilityConverter}}"> </Button> 
    <Button Content="Cambia" Width="50" Height="50" Margin="56,134,411,135" Click="Button_Click"/> 
</Grid> 

これは私のXAMLコードです。 愚かな例を開始するには、2つのボタンがあり、ボタン "Cambia"でブール値vButton =!vButtonを設定しますが、vButtonがfalseの場合も最初のボタンは非表示になります。 私のコードには何がありませんか?

この私のコンバータである

public class BoolToVisibilityConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, 
     object parameter, CultureInfo culture) 
    { 
     return (bool)value ? Visibility.Visible : Visibility.Hidden; 
    } 

    public object ConvertBack(object value, Type targetType, 
     object parameter, CultureInfo culture) 
    { 
     return (Visibility)value == Visibility.Visible; 
    } 
} 

MainWindows.xaml.cs

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 

    private bool vButton; 

    public event PropertyChangedEventHandler PropertyChanged; 

    public bool VButton 
    { 
     get 
     { 
      return vButton; 
     } 
     set 
     { 
      if (value != vButton) 
      { 
       this.vButton = value; 
       NotifyPropertyChanged("VButton"); 
      } 
     } 
    } 

    private void NotifyPropertyChanged(String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 

    } 


    private void Button_Click(object sender, RoutedEventArgs e) 
    { 

     vButton = !vButton; 
    } 

} 
+2

はあなたにも私達にあなたのViewModelコードを表示することができますか? –

+1

あなたは 'DataContext'をバインドしましたか? –

+0

ここで、(namespace)BoolToVisibilityConverterは存在しますか? – mindOfAi

答えて

1

コンバータは正常に見えます。

ここで最も一般的なシナリオは、バインドするプロパティが変更通知を発行していないことです。たとえば:

using System.ComponentModel; 
using System.Runtime.CompilerServices; 

public class MyModel : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    private bool _isButtonVisible; 

    public bool vButton 
    { 
     get { return _isButtonVisible; } 
     set 
     { 
      if (value == _isButtonVisible) 
       return; 
      _isButtonVisible = value; 
      OnPropertyChanged(); 
     } 
    } 

    private void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+0

を追加し、Xamlに何が変更されましたか? –

+0

実際にこのコントロールのDataContextをビューモデルにバインドしていますか? – LordWilmore

+0

動作しない場合は、getter、setter、およびコンバーターの各プロパティにブレークポイントを設定します。おそらく何かがヒットしていない可能性があります。これは、あなたの欠けていることを教えてくれます。 –

0

あなたのコンストラクタでDataContextの

を設定するには:MVVMパターンでは

public MainWindow() 
{ 
    InitializeComponent(); 
    DataContext = this; 
} 

を、あなたのWindowsとは別にのViewModelを定義する必要があります。 例:あなたのケースで:あなたのMainWindow.xaml.csで次に

public class MainWindowViewModel : INotifyPropertyChanged 
{ 
    private bool vButton; 
    public event PropertyChangedEventHandler PropertyChanged; 
    public bool VButton 
    { 
     get 
     { 
      return vButton; 
     } 
     set 
     { 
      if (value != vButton) 
      { 
       this.vButton = value; 
       NotifyPropertyChanged("VButton"); 
      } 
     } 
    } 

    private void NotifyPropertyChanged(String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

public MainWindow() 
{ 
    InitializeComponent(); 
    DataContext = new MainWindowViewModel(); 
}