私はいくつかのビュー(xaml)を持つWPF C#アプリケーションを持っています。このビューには、すべてのボタンを非表示にするチェックボックスがあります。WPFアプリケーションのすべてのボタンを非表示にする
問題は、それを行う方法ですか?
MainStyle.xaml
はResourceDictionary
であり、すべてのスタイル、コンバーターなどが含まれています。私のアプローチは、このファイルのスタイルを次のように設定することです:
これは機能しますが、ボタンが表示されているかどうかをユーザーが判断する必要があります。だから私はチェックボックスにスタイルをバインドする必要があります。
最初の手順は、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をオプションビューに追加します。
最初にVS出力ウィンドウで、失敗したバインディングを検索してください。スヌープを起動し、ボタンのVisibilityプロパティをチェックして、失敗したかどうかを確認します(赤で強調表示)。実際に何が失敗したのかを知ることができれば、正しい解決策を立てることができます。 – LordWilmore