2016-11-15 114 views
0

我有一個WPF C#應用程序,其中包含一些視圖(xaml),尤其是選項視圖。在這個視圖中有一個複選框來隱藏所有按鈕。隱藏WPF應用程序中的所有按鈕

問題,該怎麼做?

我有一個MainStyle.xaml這是一個ResourceDictionary幷包含所有樣式,轉換器等。我的方法是設置樣式在這個文件中,如:

<Style TargetType="Button"> 
    <Setter Property="Visibility" Value="Collapsed" /> 
</Style> 

這工作,但用戶必須決定是否按鈕是可見或不可見。所以我必須將樣式綁定到複選框。

第一步將把樣式綁定到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輸出窗口,以搜索失敗的綁定。然後啓動Snoop並檢查Visibility屬性中是否有失敗的按鈕(以紅色突出顯示)。如果你能發現真正的失敗,那麼你可以開始正確的解決方案。 – LordWilmore

回答

0

如果您無法直接從綁定中找到複選框,那麼帶有繼承的附加屬性將適用於您。例如,下面的XAML:

<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> 

使用該附加屬性:

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

爲了控制按鈕可見性。

請注意,附加屬性指定FrameworkPropertyMetadataOptions.Inherits,並且組合框綁定到名爲View(即頂層用戶控件)的元素上的此屬性。然後,按鈕樣式將綁定到每個單獨按鈕級別的此屬性(使用RelativeSource={RelativeSource Self}),該按鈕位於UserControl內部,可以獲取繼承的IsVisible值。

適合我。希望能幫助到你。