- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如标题所述,在使用属性元素语法时,我无法使 KeyBinding
工作。我所说的工作是指使用 Ctrl+Del
组合键来更改列表框的背景颜色。可以使用组合键或单击按钮,两者都会调用命令,但永远不会调用命令。在 Debug模式下设置断点时,它永远不会遇到。
我关注了 InputBinding Class example从文档中获取,并且只能在使用 UserControl
时让 KeyBinding
工作,并且想了解为什么,以及什么 我做错了。
下面是当使用属性元素语法声明的代码不起作用时的 MVCE。注释掉的是 UserControl
的一行,它封装了 StackPanel
并允许 KeyBinding
工作。取决于在 MainWindow.xaml.cs
的代码后面注释掉每个 PropertyElementSyntax
区域并取消注释每个 UserControlSyntax
区域。
主窗口.xaml:
<Window x:Class="LearningKeyBindingWPFApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LearningKeyBindingWPFApp"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="300">
<!--<local:UserControl1 x:Name="CustomColorPicker" />-->
<StackPanel Margin="0,40,0,0">
<StackPanel.InputBindings>
<KeyBinding Command="{Binding ChangeColorCommand}"
CommandParameter="{Binding ElementName=ColorPicker, Path=SelectedItem}"
Key="{Binding ChangeColorCommand.Key}"
Modifiers="{Binding ChangeColorCommand.ModifierKeys}" />
<MouseBinding Command="{Binding ChangeColorCommand}"
CommandParameter="{Binding ElementName=ColorPicker, Path=SelectedItem}"
MouseAction="{Binding ChangeColorCommand.MouseAction}" />
</StackPanel.InputBindings>
<Button Content="Change Color"
Command="{Binding ChangeColorCommand}"
CommandParameter="{Binding ElementName=ColorPicker, Path=SelectedItem}" />
<ListBox Name="ColorPicker"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
SelectedIndex="0">
<sys:String>Red</sys:String>
<sys:String>Green</sys:String>
<sys:String>Blue</sys:String>
<sys:String>Yellow</sys:String>
<sys:String>Orange</sys:String>
<sys:String>Purple</sys:String>
</ListBox>
</StackPanel>
</Window>
MainWindow.xaml.cs 的代码隐藏:
public MainWindow()
{
DataContext = this;
InitializeComponent();
InitializeCommand();
#region UserControlSyntax
//CustomColorPicker.ColorPicker.Focus();
#endregion
#region PropertyElementSyntax
ColorPicker.Focus();
#endregion
}
public SimpleDelegateCommand ChangeColorCommand { get; private set; }
private SolidColorBrush _originalColor;
private void InitializeCommand()
{
#region UserControlSyntax
//_originalColor = (SolidColorBrush)CustomColorPicker.ColorPicker.Background;
#endregion
#region PropertyElementSyntax
_originalColor = (SolidColorBrush)ColorPicker.Background;
#endregion
ChangeColorCommand = new SimpleDelegateCommand(ChangeColor)
{
Key = Key.Delete,
ModifierKeys = ModifierKeys.Control
};
}
private void ChangeColor(object colorString)
{
if (colorString == null)
{
return;
}
var selectedColor = SelectedColor((string)colorString);
#region UserControlSyntax
//if (CustomColorPicker.ColorPicker.Background == null)
//{
// CustomColorPicker.ColorPicker.Background = selectedColor;
// return;
//}
//CustomColorPicker.ColorPicker.Background = ((SolidColorBrush)CustomColorPicker.ColorPicker.Background).Color == selectedColor.Color
// ? _originalColor
// : selectedColor;
#endregion
#region PropertyElementSyntax
if (ColorPicker.Background == null)
{
ColorPicker.Background = selectedColor;
return;
}
var isColorIdentical = ((SolidColorBrush)ColorPicker.Background).Color == selectedColor.Color;
ColorPicker.Background = isColorIdentical
? _originalColor
: selectedColor;
#endregion
}
private SolidColorBrush SelectedColor(string value)
{
#region UserControlSyntax
//var selectedColor = (Color)ColorConverter.ConvertFromString(value);
#endregion
#region PropertyElementSyntax
var selectedColor = (Color)ColorConverter.ConvertFromString((string)ColorPicker.SelectedItem);
#endregion
return new SolidColorBrush(selectedColor);
}
最佳答案
问题在于,在无UserControl
的情况下,DataContext
是在命令对象初始化之前设置的。
WPF 具有强大的绑定(bind)系统,但它通常依赖于通过 INotifyPropertyChanged
进行的属性更改通知。只要您获得正确的操作顺序,某些场景在没有它的情况下也可以工作。但是,如果没有属性更改通知,如果您错过了向 WPF 提供某些属性值的机会窗口,它不会稍后再试。
当您使用 UserControl
时,UserControl
的绑定(bind)初始化发生在您设置 ChangeColorCommand
属性之后。这只是 WPF 如何初始化 UI 树中的各种对象的产物。但这意味着当 UserControl
的绑定(bind)查看 ChangeColorCommand
属性时,它具有您想要的值。
另一方面,当您将 StackPanel
显式放入窗口的 XAML 中时,为 WPF 设置属性以查看它时为时已晚。它已经在 InitializeComponent()
调用期间解析了这些绑定(bind)。稍后设置该属性无效。
根据您现在拥有的代码,您可以通过多种方式解决该问题:
DataContext = this;
的赋值移动到调用 InitializeCommand()
之后。更新 DataContext
也需要 WPF 更新所有依赖绑定(bind),因此在 InitializeCommand()
调用之后执行此操作可确保属性具有您想要的值。MainWindow
类中实现 INotifyPropertyChanged
,并在设置时引发 ChangeColorCommand
属性的 PropertyChanged
事件它。这将使 WPF 知道该值已更改,并且它应该重新评估依赖于它的任何绑定(bind)。综上所述,我会更进一步:
INotifyPropertyChanged
和 ChangeColorCommand
实现适当的 View 模型对象,并使用 that 作为数据上下文。让您的 UI 对象执行双重任务,因为 UI 和属性绑定(bind)源(即 View 模型的工作)不适合普通的 WPF 模型,牺牲了 MVVM 通常提供的好处,当然也引入了这种奇怪的计时属性绑定(bind)未按预期工作的原因并不明显。
好的,从技术上讲,您可以采用第四种方法,即在 InitializeComponent()
之前调用 InitializeCommand()
。其主要问题是,目前,它依赖于直接检索 UI 对象的属性值,并且该 UI 对象只有在调用 InitializeComponent()
之后才会存在。
这让我回到上面的 #3 选项。事实上,您不应该直接访问 UI 对象属性。这应该是你的 View 模型中的另一个属性,你应该更直接地选择初始颜色应该是什么,而不是在启动时从 UI 中获取它。
我承认,这里的设计有一些回旋余地,但您应该尽量让 View 模型和 UI 代码彼此分离。
关于c# - KeyBinding 用作 UserControl,但在 XAML 中使用属性元素语法时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57103538/
我想创建一个包含一堆其他 XAML 文件的主 XAML 文件。这可能吗? 最佳答案 在您的主 Xaml 中,使用 项目控件 balise 和 Xaml 组合器,您必须使用 定义您的项目用户控件 代替
如何在 xaml 中更改 ToggleSwitch 中实际开关的大小?我可以更改 OnContent 和 OffContent 的文本大小,但滑动开关永远不会更改大小。 这些都不会改变开关本身的大小,
我有一个文本 block 列表,其中可能包括里面的 url,比如: 构建失败,请在此处查看更多信息:http://... 构建成功 应用 http://myapp/无法启动,请在此处查看更多信息:ht
我想做的事 我最近一直在探索 XAML 资源字典。它们非常强大,但为了减少(甚至进一步)为适应任何修改而需要进行的更改,我想使用一些基本的算术运算来更改 HeightRequest Entry 的属性
我使用以下代码创建了一个弹出窗口,但我不知道如何将它居中 我试图在运行时自动更改边距,但我不知道该怎么做,但是有人知道如何将弹出窗口居中吗? 它没有标准维度,因为我需要全局化我的程序
哪种类型的命名更适合在 XAML 中使用: xmlns:inventoryControls="clr-namespace:Inventory.Components.Controls;assembly=
我正在尝试格式化我的 string每 3 位有逗号,如果不是整数,则为小数。我检查了大约 20 个示例,这是我最接近的示例: 但我得到一个 The property 'StringFormat' w
在 Windows Phone 8 中,我想将一个图像放在一个圆圈中。是否有类似网格的圆形容器?我知道有椭圆位它不是容器 最佳答案 这是我如何做到的。
有没有办法在 XAML 中检查逻辑非逻辑。我试图绑定(bind)到广泛使用的属性,但是对于这些新的绑定(bind),它的方式是错误的。我不想修改属性,那么有没有办法在 xaml 中反转 bool 结果
在 html 中可以说 width="20%"。这在 XAML 中当然是不允许的。有谁知道为什么会这样或者有没有办法在 XAML 中获得百分比值支持? 最佳答案 网格列定义和行定义允许比例单位(除了固
我可以动态创建 XAML 并将其弹出到我的应用程序中吗?该怎么做呢? 最佳答案 是的,这很简单: XmlReader tXml = XmlReader.Create(myXamlString); UI
简短版: 如果不使用 xamlc 预编译 xaml,应用程序中捆绑了什么以在运行时编译 xaml?它是否取决于 xamarin.forms nugets 之外的应用构建时的当前工具集? 长版: 我们有
我知道如何显示内容页面,但如何根据方向选择正确的布局。我想编写 xaml 文件:一个用于垂直方向,另一个用于水平方向。方向改变时是否可以切换xaml文件 最佳答案 Xamarin.Forms does
我是一名 Android 开发者,我是 Windows Phone 8 的新手 最近我做了一个 android 应用程序..所以我想在 Windows Phone 8 中开发具有相同 View 和相同
在库中编译 XAML 时出现以下错误: Themes\Generic.xaml(35,12): XamlCompiler error WMC0610: XBF generation error cod
我需要在几个不同的 XAML 布局文件中包含一个通用的 XAML。 我不想在其他 XAML 文件中对重复的 XAML 部分进行硬编码。 怎么做包含? 最佳答案 你能把共享代码变成UserControl
我想像这样添加一个符号: 这行不通,所以我尝试用\分隔,但也行不通。有人对我如何做到这一点有任何建议吗? 最佳答案 其实这个问题不是关于XAML的,它只是一个纯XML的问题。 XML中有些字符需要转
我在 Xaml 中定义了这样的列表。 #3599B8 #374649 #FD625E #F2C80F
在向 Microsoft 报告之前,我想在这里问一下。我有一个问题,我无法看到我的 WinUI 3 应用程序的实时可视化树。我什至看不到应用程序内工具栏。我可以在 WPF 和 UWP 应用程序中看到,
我已经定义了一个动画作为一个资源,当通过这样的 EventTrigger 调用时它可以工作: 然后
我是一名优秀的程序员,十分优秀!