CodeBehind 不适用于 ResourceDictionary
CodeBehind not working for ResourceDictionary
我想通过 属性 禁用我的 WPF c# 应用程序中的所有工具提示。我以这种方式为所有 Windows、UserControls 等使用 ResourceDictionary (MyStyle.xaml):
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Interface;component/MyStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
这是我的 ResourceDictionary MyStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Dupa.Interface.MyStyle">
<Style TargetType="{x:Type ToolTip}">
<Style.Setters>
<Setter Property="Visibility" Value="{Binding Path=TooltipsEnabled, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</Style.Setters>
</Style>
...
</ResourceDictionary>
和代码隐藏 MyStyle.xaml.cs
public partial class MyStyle : ResourceDictionary, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool tooltipsEnabled;
public MyStyle()
{
InitializeComponent();
TooltipsEnabled = false;
}
public bool TooltipsEnabled
{
get { return tooltipsEnabled; }
set
{
tooltipsEnabled = value;
NotifyPropertyChanged("TooltipsEnabled");
}
}
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
但绑定无效。如果我直接在 MyStyle.xaml 中设置可见性而不绑定,它会起作用:
<Style TargetType="{x:Type ToolTip}">
<Style.Setters>
<Setter Property="Visibility" Value="Collapsed" />
</Style.Setters>
</Style>
我在构建过程中遇到了一些错误:
BindingExpression 路径错误:'TooltipsEnabled' 属性 未在 'object' ''CalculationViewModel' 上找到
BindingExpression 路径错误:在 'object' ''SettingsViewModel' 上找不到 'TooltipsEnabled' 属性
BindingExpression 路径错误:'TooltipsEnabled' 属性 未在 'object' ''ControllerViewModel'
上找到
正如错误非常清楚地指出的那样,您需要在所有 ViewModel 中定义 属性 'TooltipsEnabled'。当您在其中一个视图中包含 Dictionary 'MyStyle' 时,WPF 会尝试将 属性 绑定到您当前的 DataContext 上,这很可能是包含样式的视图的相应 ViewModel。
就我个人而言,我至少会定义一个类似于 'IBaseViewModel' 的接口来定义此 属性,因此您的所有 ViewModel 都需要实现该 属性。为这种情况创建一个基础 class,您从中派生 ViewModels 也是一种选择。
你的基地 class 可能看起来像这样:
public class BaseViewModel : IBaseViewModel, INotifyPropertyChanged
{
private bool tooltipsEnabled;
public bool TooltipsEnabled
{
get { return tooltipsEnabled; }
set
{
tooltipsEnabled = value;
NotifyPropertyChanged("TooltipsEnabled");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
我想通过 属性 禁用我的 WPF c# 应用程序中的所有工具提示。我以这种方式为所有 Windows、UserControls 等使用 ResourceDictionary (MyStyle.xaml):
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Interface;component/MyStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
这是我的 ResourceDictionary MyStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Dupa.Interface.MyStyle">
<Style TargetType="{x:Type ToolTip}">
<Style.Setters>
<Setter Property="Visibility" Value="{Binding Path=TooltipsEnabled, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</Style.Setters>
</Style>
...
</ResourceDictionary>
和代码隐藏 MyStyle.xaml.cs
public partial class MyStyle : ResourceDictionary, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool tooltipsEnabled;
public MyStyle()
{
InitializeComponent();
TooltipsEnabled = false;
}
public bool TooltipsEnabled
{
get { return tooltipsEnabled; }
set
{
tooltipsEnabled = value;
NotifyPropertyChanged("TooltipsEnabled");
}
}
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
但绑定无效。如果我直接在 MyStyle.xaml 中设置可见性而不绑定,它会起作用:
<Style TargetType="{x:Type ToolTip}">
<Style.Setters>
<Setter Property="Visibility" Value="Collapsed" />
</Style.Setters>
</Style>
我在构建过程中遇到了一些错误:
BindingExpression 路径错误:'TooltipsEnabled' 属性 未在 'object' ''CalculationViewModel' 上找到 BindingExpression 路径错误:在 'object' ''SettingsViewModel' 上找不到 'TooltipsEnabled' 属性 BindingExpression 路径错误:'TooltipsEnabled' 属性 未在 'object' ''ControllerViewModel'
上找到正如错误非常清楚地指出的那样,您需要在所有 ViewModel 中定义 属性 'TooltipsEnabled'。当您在其中一个视图中包含 Dictionary 'MyStyle' 时,WPF 会尝试将 属性 绑定到您当前的 DataContext 上,这很可能是包含样式的视图的相应 ViewModel。
就我个人而言,我至少会定义一个类似于 'IBaseViewModel' 的接口来定义此 属性,因此您的所有 ViewModel 都需要实现该 属性。为这种情况创建一个基础 class,您从中派生 ViewModels 也是一种选择。
你的基地 class 可能看起来像这样:
public class BaseViewModel : IBaseViewModel, INotifyPropertyChanged
{
private bool tooltipsEnabled;
public bool TooltipsEnabled
{
get { return tooltipsEnabled; }
set
{
tooltipsEnabled = value;
NotifyPropertyChanged("TooltipsEnabled");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}