有时我们使用复杂的方法很多次,我们忘记了完成任务的最简单方法.
我知道如何进行命令绑定,但我总是使用相同的方法.
创建一个实现ICommand接口的类,并从视图模型中创建该类的新实例,绑定就像一个魅力.
这是我用于命令绑定的代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
testCommand = new MeCommand(processor);
}
ICommand testCommand;
public ICommand test
{
get { return testCommand; }
}
public void processor()
{
MessageBox.Show("hello world");
}
}
public class MeCommand : ICommand
{
public delegate void ExecuteMethod();
private ExecuteMethod meth;
public MeCommand(ExecuteMethod exec)
{
meth = exec;
}
public bool CanExecute(object parameter)
{
return false;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter) …Run Code Online (Sandbox Code Playgroud) 是否可以触发命令以通知窗口已加载.另外,我没有使用任何MVVM框架(在某种意义上的框架,Caliburn,Onxy,MVVM Toolkit等)
我有一个UserControl,它将CommandBinding添加到它的CommandBindings集合来处理特定的Command.后来我在一个窗口中使用此控件,并希望向该控件添加另一个绑定以添加其他行为.但问题是,当我这样做时,似乎当我将另一个CommandBinding添加到控件的CommandBindings集合时,它取代了已经为同一个Command创建的任何绑定.那么看起来控件每个控件只能有一个CommandBinding,这是正确的吗?
请参阅下面的代码示例,该示例尝试为同一个Save Command设置两个CommandBindings.
<Window x:Class="MultipleCommandBindings.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.CommandBindings>
<CommandBinding Command="Save"
Executed="CommandBinding_Executed" />
<CommandBinding Command="Save"
Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Grid>
<Button Height="23"
HorizontalAlignment="Right"
Margin="0,0,25,88"
Name="button1"
VerticalAlignment="Bottom"
Width="75"
Command="Save">Button</Button>
</Grid>
Run Code Online (Sandbox Code Playgroud)
最初我在编写此代码时期望编译时或运行时异常,但对它没有抱怨感到惊讶.接下来虽然我很失望,因为我的CommandBinding_Executed处理程序只被调用一次而不是我希望的两次.
更新: 经过一些测试后,看来我的第二个CommandBinding没有覆盖我的第一个,但是看起来即使我没有在我的事件处理程序中将Handled设置为true,第一个命令绑定吞下了Command.我很确定在这一点上我的问题的解决方案是理解为什么路由命令没有传播通过第一个处理程序,即使Handled未设置为true.
更新: 我发现这个很好的小信息只是证实了WPF中Command路由背后的一些奇怪行为.
更新: 考虑如何解决以下事实:每个命令似乎只有一个有效的CommandBinding,默认的CommandBinding类似乎将Executed和CanExecute暴露为事件,当然所有事件都可以有多个处理程序.然后,我们的想法是使用标准CommandBindings.Add方法之外的其他方法向命令添加其他处理程序.也许这可以通过Control类上的扩展方法完成,并与自定义CompositeCommandBinding类结合使用,该类允许我们在一个主绑定中聚合多个绑定.
在WPF应用程序中,我正在尝试绑定一个Command以使用快捷键在应用程序中的任何位置启动计算器工具表单,我创建了一个命令,但没有获得如何映射命令和快捷键以在我的应用程序中创建通用快捷键.提前致谢.
我有一个ToggleButton.我正在使用命令绑定,我想将其IsChecked属性的值作为参数传递.如何在不命名ToggleButton并使用其名称来解决自身问题的情况下如何做到这一点?
目前我通过命名控件来解决这个问题,但我认为这可以做得更好吗?
<ToggleButton x:Name="_myToggle"
Command="{Binding SomeCommand}"
CommandParameter="{Binding ElementName=_myToggle, Path=IsChecked}">
Apply Toggle
</ToggleButton>
Run Code Online (Sandbox Code Playgroud) 如何使用<Interaction.Behaviors>和<EventTriggerBehavior>在Windows 10通用的应用程序?我会用Blend得到这样的东西:
上述两张图片:Jef Daels 2015
在may(bad)文档中,我已经读过它必须是这里的东西:
以下是我需要做的代码:
<ListBox Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3"
ItemContainerStyle="{StaticResource lstidflt}" SelectedItem="{Binding SelectedVillage, Mode=TwoWay}"
ItemTemplate="{StaticResource weatheritemdt}" ItemsSource="{Binding VillageList}" >
</ListBox>
Run Code Online (Sandbox Code Playgroud) 当我创建一个具有CommandBinding到RoutedUICommand的用户控件我担心我得到内存泄漏.
场景:
将RoutedUICommand作为c类中的静态存储我的命令在用户控件上实现CommandBindings.将用户控件添加到主窗体.从主窗体中删除用户控件,将对它的引用设置为null.
命令绑定的canExecute继续触发.我没有对UserControl的引用,所以它泄露了.表格关闭后,它会长时间保持射击状态.(我没有看到它停止)如果我强制收集垃圾,它会被收集(canExecute停止开火)
我有一个测试项目,说明了这一点.我在canExecute中有一个Console.WriteLine,它打印出触发该方法的对象的哈希码.它有一个用于添加新用户控件的按钮和一个用于删除它的按钮.
我不应该关心这个吗?如果强制,则会收集用户控件.这是否意味着它会在下一个系列中收集?我注意到我们的应用程序性能下降,并且正在跟踪内存泄漏等.我们有许多ui元素的复杂表单,当从布局中删除时,它们会闲置处理器和内存空间.(我们使用了很多命令)我认为一旦从可视树中删除了某些内容,就无法再接收路由事件了.我错过了什么?
WPF Treeview响应+并-按键以展开和折叠树中的节点.大!
是否有现有命令我可以绑定我的工具栏按钮或菜单项以在树视图中执行相同的操作?我没有看到与stock命令常量中的扩展/折叠有关的任何内容.
如何在windows phone 8中访问父元素的datacontext?WP8中没有AncestorType.
<ItemsControl x:Name="Elements" ItemsSource="{Binding MyList}" Grid.Row="2" Grid.Column="3">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="e" Width="100" Height="100" Command="{Binding MyCommand" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
"MyCommand"在"MyList"之外定义.那么如何从我的按钮访问根数据文本(DataContext = MyClass).MyCommand在MyClass类中定义.
提前致谢.
我需要为WPF应用程序中的网格之间的数据实现数据(非文本或CSV)的自定义复制+剪切+粘贴.使用标准ApplicationCommands并定义CommandBinding非常有效,但前提是DataGrid包含至少1行数据以及何时选择.当没有行或焦点不在任何行上时,所有命令都被禁用.
为了解决这个问题,我尝试在DataGrid上调用CommandManager.InvalidateRequerySuggested()并设置Focusable = true和/或FocusManager.IsFocusScope = true,但似乎内部DataGrid作为一个整体在处理复制/粘贴操作时"不感兴趣",只有它的行正在重新查询命令CanExecute状态并相应地调用Execute.它也忽略了KeyBindings.
如何让DataGrid处理重新查询ApplicationCommands?
请找到我在下面测试问题的示例:
<Window x:Class="WpfApplication1.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">
<DataGrid x:Name="TheGrid">
<DataGrid.Columns>
<DataGridTextColumn Header="Number" Binding="{Binding}"/>
</DataGrid.Columns>
<DataGrid.InputBindings>
<KeyBinding Key="A" Command="{x:Static ApplicationCommands.New}"/>
</DataGrid.InputBindings>
<DataGrid.CommandBindings>
<CommandBinding Command="{x:Static ApplicationCommands.Paste}" CanExecute="CanPaste" Executed="Paste"/>
<CommandBinding Command="{x:Static ApplicationCommands.Copy}" CanExecute="CanCopy" Executed="Copy"/>
<CommandBinding Command="{x:Static ApplicationCommands.New}" CanExecute="CanAddNew" Executed="AddNew"/>
</DataGrid.CommandBindings>
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Command="{x:Static ApplicationCommands.Copy}" Header="Copy"/>
<MenuItem Command="{x:Static ApplicationCommands.Paste}" Header="Paste"/>
<MenuItem Command="{x:Static ApplicationCommands.New}" Header="New row"/>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
</Window>
Run Code Online (Sandbox Code Playgroud)
而背后的代码:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public partial class MainWindow …Run Code Online (Sandbox Code Playgroud) commandbinding ×10
wpf ×8
.net ×2
c# ×2
xaml ×2
binding ×1
blend ×1
command ×1
data-binding ×1
icommand ×1
memory-leaks ×1
mvvm ×1
treeview ×1
windows ×1
wpfdatagrid ×1