2016-11-15 5 views
0

私はいくつかのビュー(xaml)を持つWPF C#アプリケーションを持っています。このビューには、すべてのボタンを非表示にするチェックボックスがあります。WPFアプリケーションのすべてのボタンを非表示にする

問題は、それを行う方法ですか?

MainStyle.xamlResourceDictionaryであり、すべてのスタイル、コンバーターなどが含まれています。私のアプローチは、このファイルのスタイルを次のように設定することです:

これは機能しますが、ボタンが表示されているかどうかをユーザーが判断する必要があります。だから私はチェックボックスにスタイルをバインドする必要があります。

最初の手順は、ResourceDictionary(MainStyle.xaml)のコードの背後にスタイルをバインドすることです。しかし、それは動作しません。コンストラクタのプロパティをfalseに設定しましたが、ボタンが表示されます。

MainStyle.xaml

<Style TargetType="Button"> 
    <Setter Property="Visibility" 
      Value="{Binding ButtonsEnabled, RelativeSource={RelativeSource AncestorType=ResourceDictionary}, Converter={StaticResource BooleanVisibilityConverter}}" /> 
</Style> 

コードの後ろ(MainStyle.xaml.cs)

public partial class BaseStyle : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    public BaseStyle() 
    { 
     InitializeComponent(); 
     ButtonsEnabled = false; // for testing 
    } 

    private Boolean buttonsEnabled; 


    public bool ButtonsEnabled 
    { 
     get { return buttonsEnabled; } 
     set 
     { 
      buttonsEnabled = value; 
      NotifyPropertyChanged("ButtonsEnabled"); 
     } 
    } 

    private void NotifyPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

結合作品ならば、第二段階は、中にスタイルをバインドすることですResourceDictionaryをオプションビューに追加します。

+0

最初にVS出力ウィンドウで、失敗したバインディングを検索してください。スヌープを起動し、ボタンのVisibilityプロパティをチェックして、失敗したかどうかを確認します(赤で強調表示)。実際に何が失敗したのかを知ることができれば、正しい解決策を立てることができます。 – LordWilmore

答えて

0

バインディングから直接チェックボックスを見つけることができない場合は、継承された添付プロパティが有効です。たとえば、次のXAML:ボタンの可視性を制御するには

public static class ButtonVisibility 
{ 
    public static readonly DependencyProperty IsVisibleProperty = DependencyProperty.RegisterAttached("IsVisible", typeof(bool), typeof(ButtonVisibility), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits)); 

    public static bool GetIsVisible(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(IsVisibleProperty); 
    } 

    public static void SetIsVisible(DependencyObject obj, bool value) 
    { 
     obj.SetValue(IsVisibleProperty, value); 
    } 
} 

<UserControl x:Class="WpfSpikes.InheritedAttachPropertyView" x:Name="View" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:WpfSpikes" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <UserControl.Resources> 
     <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/> 
     <Style x:Key="VisibleButtonStyle" TargetType="{x:Type Button}"> 
      <Setter Property="Visibility" Value="{Binding RelativeSource={RelativeSource Self}, Path=(local:ButtonVisibility.IsVisible), Converter={StaticResource BooleanToVisibilityConverter}}"/> 
     </Style> 
    </UserControl.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <CheckBox Content="Show Buttons?" IsChecked="{Binding ElementName=View, Path=(local:ButtonVisibility.IsVisible), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
     <Button Grid.Row="1" Content="Button1" Style="{StaticResource VisibleButtonStyle}"/> 
     <Button Grid.Row="2" Content="Button2" Style="{StaticResource VisibleButtonStyle}"/> 
     <Button Grid.Row="3" Content="Button3" Style="{StaticResource VisibleButtonStyle}"/> 
     <Button Grid.Row="4" Content="Button4" Style="{StaticResource VisibleButtonStyle}"/> 
    </Grid> 
</UserControl> 

がこの添付プロパティを使用します。

添付プロパティがFrameworkPropertyMetadataOptions.Inheritsを指定し、コンボボックスがViewという名前の要素(つまり最上位のユーザーコントロール)のこのプロパティにバインドされていることに注意してください。ボタンスタイルは、個々のボタン(RelativeSource={RelativeSource Self}を使用)のレベルでこのプロパティにバインドされます。ボタンはUserControl内にあり、継承されたIsVisibleの値を取得します。

私のために働きます。それが役に立てば幸い。

関連する問題