gpt4 book ai didi

wpf - 在 XAML 中,VisualStateManager 的正确 XML 命名空间是什么?

转载 作者:行者123 更新时间:2023-12-01 11:55:54 26 4
gpt4 key购买 nike

我正在尝试重新设计几个 ToggleButton 的样式。显然我不能简单地将背景设置为新颜色,因为有一个“控制模板”提供了 ToggleButton 的视觉行为。所以我需要做的是在 XAML 中为 ToggleButton 指定一个替换“ControlTemplate”,它提供不同的视觉行为,而不仅仅是简单的背景颜色。
Q1。这个对吗?


我想从 ToggleButton 的“默认”控制模板开始,我捕获了它 from here ,然后进行修改。实际上这是 Silverlight 的默认 ControlTemplate,我猜,我没有使用 Silverlight,我使用的是 WPF。但是... The corresponding doc page for WPF不包括默认控制模板的规范。它提供了“一个”ControlTemplate,这不是我想要的。
Q2。我不确定我使用 Silverlight 的东西是否重要。是吗?


在 Silverlight 示例中,有一个 vsm 的 XML namespace 前缀应用于 VisualStateManager。显然 xml 命名空间是

  xmlns:vsm = "clr-namespace:System.Windows;assembly=System.Windows"  

...但是我在其他地方读到这个 ​​XML namespace “不再需要”。

这一切都非常令人困惑。

在谷歌空间,there are references to something called "The WPF toolkit"我之前接触过 - I used it for an autocomplete textbox在 WPF V4 发布之前。我猜测 WPF 工具包的一些内容已被整合到 WPF for .NET v4.0 中,这就是为什么我不再需要指定 WPF 工具包的原因。 Q3。如果有人能证实这种理解,我将不胜感激。


好的,现在从 ToggleButton 的“默认”ControlTemplate 开始,我的第一步是编译它,然后再进行任何更改。它不编译,失败

c:\dev...\ToggleButtonStyle1.xaml(23,14): error MC3074: The tag 'VisualStateManager.VisualStateGroups' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'. Line 23 Position 14.

足够清楚了。然后我查看了在 XAML 中指定 VisualStateManager 的文档。令人困惑的是,它指定了两个 xml namespace ,其中之一是我实际使用的 namespace 。

enter image description here

Q4 嗯,我应该使用哪些?其中之一,我确实使用过,但没有用。文档完全不清楚指定两个 XML namespace 的含义。 (砍掉他们的脑袋!)

我在项目文件中引用了 PresentationFramework.dll:

  <ItemGroup>
....
<Reference Include="PresentationFramework" />
</ItemGroup>

我在这里没有使用 Visual Studio;我正在使用文本编辑器。我想了解它是如何工作的,而不是要按什么按钮。

感谢大家提供的任何帮助。


只是附带评论 - 这一切看起来非常非常复杂。我想要做的就是在打开时更改 ToggleButton 的颜色。它真的不应该这么复杂。

最佳答案

您不需要为 VSM 指定命名空间(http://schemas.microsoft.com/winfx/2006/xaml/presentation 命名空间是默认的 WPF 命名空间,在大多数标准 .xaml 中声明为 xmlns="...")——但是您只能使用它在视觉层次结构的某些部分。

例如,当我在标准用户控件中使用 VSM 时,它看起来像这样:

<UserControl x:Class="Whatever"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="LayoutRoot">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Disabled">
<!-- Storyboards go here -->
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</UserControl>

将 VSM xaml 放置在此级别将使您的 Storyboard能够引用网格中包含的任何元素。这在 ControlTemplate 中的工作方式与您正在使用的一样。不过需要注意的一件事是,在您自己的 UserControls 中,您可以随意命名视觉状态(因为您最终会在代码中调用以切换到该视觉状态),使用内置控件,您的视觉状态必须准确命名为控件所期望的。

关于wpf - 在 XAML 中,VisualStateManager 的正确 XML 命名空间是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7324795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com