ResourceDictionary 重新加载后,工具提示内容中的绑定无法正常工作
Bindings in ToolTip content fail to work after ResourceDictionary reload
我想创建一个包含自定义内容的工具提示。因为我想保留 ToolTip class 的默认行为和样式,所以我不想定义它的模板,而是为其分配自定义内容。我决定创建一个 ToolTip class 后代,它具有将从自定义内容中引用的特定依赖属性。这是一个例子:
Public Class CustomToolTip
Inherits ToolTip
Public Shared Property Value1Property As DependencyProperty = DependencyProperty.Register("Value1", GetType(String), GetType(CustomToolTip), New PropertyMetadata(""))
Public Property Value1 As String
Get
Return GetValue(Value1Property)
End Get
Set(value As String)
SetValue(Value1Property, value)
End Set
End Property
End Class
我在单独的资源字典文件中定义了这样的内容:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
<Style x:Key="ToolTipContent" TargetType="local:CustomToolTip">
<Setter Property="Content">
<Setter.Value>
<StackPanel>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=local:CustomToolTip}, Path=Value1}" />
</StackPanel>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
资源字典文件是从使用CustomToolTipclass的控件中引用的,例如:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:local="clr-namespace:WpfApplication1">
<Window.Resources>
<ResourceDictionary x:Name="resourceDict">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CustomToolTip.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Canvas>
<Button x:Name="myButton" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75">
<Button.ToolTip>
<local:CustomToolTip Value1="Test" Style="{DynamicResource ToolTipContent}" />
</Button.ToolTip>
</Button>
</Canvas>
</Window>
这工作得很好,但直到我重新加载控件的资源字典。我在我的生产应用程序中这样做,因为它使用用户可以在运行时更改的自定义主题。我像这样重新加载字典(我使用合并字典,因为在我的生产应用程序中资源存储在几个文件中):
resourceDict.Clear()
Dim src As New Uri("C:\Users\romico\AppData\Local\Temporary Projects\WpfApplication1\Themes\CustomToolTip.xaml", UriKind.RelativeOrAbsolute)
Dim dict As New ResourceDictionary() With {.Source = src}
resourceDict.MergedDictionaries.Add(dict)
重新加载字典后,工具提示不再显示任何内容。似乎由于某种原因,对 Value1 的绑定停止工作了。
提一下,运行时加载的资源文件和上面的不一样,属于项目。外部声明 'local' 命名空间,包括程序集名称:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1;assembly=WpfApplication1">
如果你能帮我解决问题,我将不胜感激。谢谢!
我定义了 ContentTemplate 而不是 Content,这解决了问题。但是,我不明白为什么(我是 WPF 的初学者)。如果有人能解释为什么这与上述第一种方法相反,我将不胜感激。谢谢!
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
<Style x:Key="ToolTipContent" TargetType="local:CustomToolTip">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=local:CustomToolTip}, Path=Value1}" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
我想创建一个包含自定义内容的工具提示。因为我想保留 ToolTip class 的默认行为和样式,所以我不想定义它的模板,而是为其分配自定义内容。我决定创建一个 ToolTip class 后代,它具有将从自定义内容中引用的特定依赖属性。这是一个例子:
Public Class CustomToolTip
Inherits ToolTip
Public Shared Property Value1Property As DependencyProperty = DependencyProperty.Register("Value1", GetType(String), GetType(CustomToolTip), New PropertyMetadata(""))
Public Property Value1 As String
Get
Return GetValue(Value1Property)
End Get
Set(value As String)
SetValue(Value1Property, value)
End Set
End Property
End Class
我在单独的资源字典文件中定义了这样的内容:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
<Style x:Key="ToolTipContent" TargetType="local:CustomToolTip">
<Setter Property="Content">
<Setter.Value>
<StackPanel>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=local:CustomToolTip}, Path=Value1}" />
</StackPanel>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
资源字典文件是从使用CustomToolTipclass的控件中引用的,例如:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:local="clr-namespace:WpfApplication1">
<Window.Resources>
<ResourceDictionary x:Name="resourceDict">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CustomToolTip.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Canvas>
<Button x:Name="myButton" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75">
<Button.ToolTip>
<local:CustomToolTip Value1="Test" Style="{DynamicResource ToolTipContent}" />
</Button.ToolTip>
</Button>
</Canvas>
</Window>
这工作得很好,但直到我重新加载控件的资源字典。我在我的生产应用程序中这样做,因为它使用用户可以在运行时更改的自定义主题。我像这样重新加载字典(我使用合并字典,因为在我的生产应用程序中资源存储在几个文件中):
resourceDict.Clear()
Dim src As New Uri("C:\Users\romico\AppData\Local\Temporary Projects\WpfApplication1\Themes\CustomToolTip.xaml", UriKind.RelativeOrAbsolute)
Dim dict As New ResourceDictionary() With {.Source = src}
resourceDict.MergedDictionaries.Add(dict)
重新加载字典后,工具提示不再显示任何内容。似乎由于某种原因,对 Value1 的绑定停止工作了。
提一下,运行时加载的资源文件和上面的不一样,属于项目。外部声明 'local' 命名空间,包括程序集名称:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1;assembly=WpfApplication1">
如果你能帮我解决问题,我将不胜感激。谢谢!
我定义了 ContentTemplate 而不是 Content,这解决了问题。但是,我不明白为什么(我是 WPF 的初学者)。如果有人能解释为什么这与上述第一种方法相反,我将不胜感激。谢谢!
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
<Style x:Key="ToolTipContent" TargetType="local:CustomToolTip">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=local:CustomToolTip}, Path=Value1}" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>