有没有办法确定是否ToggleButton通过DelegateCommands 检查/取消检查?
TIA,迈克
下面的XAML代码.我正在使用ItemsControl并绑定到一个集合.我基本上想要一种方法来获得每个按钮点击时的切换状态.
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding Modifiers, Mode=TwoWay}">
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto">
<WrapPanel Margin="10" Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
FlowDirection="LeftToRight" IsItemsHost="true">
</WrapPanel>
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton FontSize="18" Opacity="0.8"
Command="{Binding DataContext.ModifierToggleCommand,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Views:ModifiersView}}}"
CommandParameter="{Binding}" Height="80" Width="200" Margin="5"
Content="{Binding Path=ModifierName}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud) 我完全迷失在MVVM中使用的命令绑定中.我应该如何将我的对象绑定到窗口和/或它的命令控件来获取方法Button Click?
这是一CustomerViewModel堂课:
public class CustomerViewModel : ViewModelBase
{
RelayCommand _saveCommand;
public ICommand SaveCommand
{
get
{
if (_saveCommand == null)
{
_saveCommand = new RelayCommand(param => this.Save(), param => this.CanSave);
NotifyPropertyChanged("SaveCommand");
}
return _saveCommand;
}
}
public void Save()
{
...
}
public bool CanSave { get { return true; } }
...
Run Code Online (Sandbox Code Playgroud)
ViewModelBase实现INotifyPropertyChanged接口以下是Button命令的绑定方式:
<Button Content="Save" Margin="3" Command="{Binding DataContext.Save}" />
Run Code Online (Sandbox Code Playgroud)
将一个实例CustomerViewModel分配给DataContext包含a的窗口Button.
给定的示例不起作用:我已将断点放入Save方法中,但执行不会传递给方法.我已经看到很多例子(在stackoverflow上也是如此),但是无法弄清楚应该如何指定绑定. …
我有一个带有命令绑定和使用Prism库的文本块.
这是XAML的一部分:
<TextBlock Margin="0,10,0,0">SocialSecurityNumer:</TextBlock>
<TextBox Name="SSNText" GotFocus="TextBox_GotFocus" Text="{Binding SSN, UpdateSourceTrigger=PropertyChanged}" Margin="0,3,0,0"/>
Run Code Online (Sandbox Code Playgroud)
这是后面的ViewModel:
public FindViewModel()
{
var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
FindCommand = new DelegateCommand(
() => eventAggregator.GetEvent<SSNChangedEvent>().Publish(SSN),
() => !string.IsNullOrWhiteSpace(Kennitala)
);
}
public DelegateCommand FindCommand { get; set; }
private string ssn;
public string SSN
{
get { return ssn; }
set
{
if (ssn== value)
return;
ssn = value;
RaisePropertyChanged(() => SSN);
FindCommand.RaiseCanExecuteChanged();
}
}
Run Code Online (Sandbox Code Playgroud)
这是GridViewModel,它监听此事件触发器并使用SSN作为参数启动一个函数
public class GridViewModel : NotificationObject
{
public GridViewModel()
{
var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
eventAggregator.GetEvent<SSNChangedEvent>().Subscribe(GetData); …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个关于如何使用 Prism 实现事件聚合器的好(读:简单)示例。我从未使用过 Prism,而且对 MVVM 本身也很陌生。
我有一个 WPF 画布作为视图,我想在 Viewmodel 中处理画布上的 MouseUp 事件。现在我们组织的权力希望我使用 Prism,显然 Prism 建议使用事件聚合器,这就是为什么我需要一个示例来开始。
我没有成功从ListView项发送CommandParameter.我的代码如下.
<ListView x:Name="myList" ItemsSource="{Binding MyData}"
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding Path=DataContext.MyCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" CommandParameter="{Binding SelectedItem, ElementName=myList}" >
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=SomeValue}" />
</StackPanel>
</Button.Content>
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)
单击ListView上的项目时,该命令被称为okay,但CommandParameter显示Nothing.这有什么问题?
ViewModel命令在这里:
Public ReadOnly Property MyData As List(Of myObject)
Get
Return _myObjectrepo.GetAll()
End Get
End Property
Public Property MyCommand As ICommand
Get
If _myCommand Is Nothing Then
_myCommand = New RelayCommandWithParameter(Of myObject)(AddressOf Navigate)
End If
Return _myCommand
End Get
Set(value As ICommand)
_myCommand …Run Code Online (Sandbox Code Playgroud) 我想按照MVVM模式,通过单击DataGrid上方(而非内部)的按钮从WPF DataGrid删除行。尽管行中的删除按钮本身可以工作,但它们有点丑陋(必须先选择该行),并且由于删除按钮旁边还有添加和编辑按钮,我认为删除按钮更适合。我的ViewModel中的相应部分如下所示:
<Button Grid.Row="0" Grid.Column="0" Content="add"/>
<Button Grid.Row="0" Grid.Column="1" Content="edit"/>
<Button Grid.Row="0" Grid.Column="2" Content="delete"/>
<DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3"
ItemsSource="{Binding dataTableListItems}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name"/>
<!--I don't wan't to use these "in-Row" delete buttons-->
<DataGridTemplateColumn Header="delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="Delete" Content="X"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
关于按钮的命令绑定看起来如何的任何想法?或者,如果始终启用行内删除按钮,并且单击一个按钮将删除相应的行而无需先选择该行,那么我也将感到满意。
我有一个"命令"类:
public static class MyCommands
{
private static ICommand exitCommand = new RoutedCommand();
public static ICommand ExitCommand { get { return exitCommand; } }
}
Run Code Online (Sandbox Code Playgroud)
MainWindow.xaml.cs中的代码隐藏:
private void BindCommands()
{
this.CommandBindings.Add(new CommandBinding(MyCommands.ExitCommand, this.Exit));
}
private void Exit(object sender, ExecutedRoutedEventArgs e)
{
Application.Current.Shutdown();
}
Run Code Online (Sandbox Code Playgroud)
实现菜单栏的用户控件中的一些XAML:
<MenuItem Header="_Exit"
Command="{x:Static local:MyCommands.ExitCommand}"
/>
Run Code Online (Sandbox Code Playgroud)
代码有效.我喜欢所涉及的一般模式,我想继续使用它.
但是,我也在努力追求其他目标,例如进行测试驱动开发,并通过我的单元和集成测试实现100%的覆盖率.我还希望100%符合StyleCop和FxCop警告.而且我被抓到了这里.
我的MainWindow.Exit()方法是私有的,正如FxCop(Microsoft.Security:CA2109)所推荐的那样,但这意味着我不能直接从测试中调用它.我想我可以把它公开并压制FxCop消息.或者我可以使用访问者.但是我反对直接针对私有方法编写测试,特别是在这种情况下,因为所有这些都是测试方法而不是命令绑定本身.
我觉得必须有其他方法从我的测试代码调用命令,以便我可以验证命令是否按预期工作(除了手动测试).有什么建议?
我在C#WPF MVVM应用程序中有以下代码.
public RelayCommand PolishCommand
{
get
{
polishcommand = new RelayCommand(e =>
{
PolishedWeightCalculatorViewModel model = new PolishedWeightCalculatorViewModel(outcomeIndex, OutcomeSelectedItem.RoughCarats);
PolishedWeightCalculatorView polish = new PolishedWeightCalculatorView(model);
bool? result = polish.ShowDialog();
if (result.HasValue)
{
Run Code Online (Sandbox Code Playgroud)
但我开始知道,从MVM模式中调用viewmodel的窗口是错误的.
也在下面的链接中说明.
请提供替代解决方案,帮助我任何人.
提前致谢.
我在我的Windows Phone 8.1中使用MVVM灯这里是代码
XAML
<TextBox Text="{Binding SearchText, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="TextChanged">
<Core:InvokeCommandAction Command="{Binding SearchTextChanged}"></Core:InvokeCommandAction>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
这是我的VM
private RelayCommand _searchTextChanged;
/// <summary>
/// Gets the SearchTextChanged.
/// </summary>
public RelayCommand SearchTextChanged
{
get
{
return _searchTextChanged
?? (_searchTextChanged = new RelayCommand(
() =>
{
LoadContents(this.SearchText);
}));
}
}
Run Code Online (Sandbox Code Playgroud)
每次更改文本时,SearchTextChanged命令都会正常触发,但SearchText属性中的文本未更新,它只减少一个字符.例如,如果文本框中的文本是A而不是SearchText包含null.如果文本框中的文本是'aaa'而不是SearchText中的文本只是'aa',则始终缺少最后一个字符.
有任何想法吗?
是否可以将多个命令绑定到按钮.
我有一个用户控件,我在我的主应用程序(父应用程序)中调用.
我想在两个控件(用户控件以及主窗口)上处理单击命令.但是我只能得到一个.
有什么方法可以让我得到这个.
任何帮助都非常感谢.
代码片段:
public class MainWindowFooterCommands
{
public static readonly RoutedUICommand FooterClickLocalCommand = new RoutedUICommand("Local Button Command", "FooterClickLocalCommand", typeof(MainWindowFooterCommands));
}
private void MainWindowFooterBindCommands()
{
CommandBinding cmdBindingBXClick = new CommandBinding(MainWindowFooterCommands.FooterClickLocalCommand);
cmdBindingBXClick.Executed += ClickCommandHandler;
CommandBindings.Add(cmdBindingBXClick);
}
void ClickCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
//Do Something
}
//Parent Control holding an instance of the footer control.
class MainWindow {
public MainWindow()
{
CommandBinding cmdBindingBXClick1 = new CommandBinding(MainWindowFooterCommands.BXClickMainWindowCommand);
cmdBindingBXClick1.Executed += LoadParent;
CommandBindings.Add(cmdBindingBXClick1);
}
public void LoadParent(object sender, ExecutedRoutedEventArgs e)
{
LoadParentWindow();
} …Run Code Online (Sandbox Code Playgroud) 我是WPF的新手,并尝试创建一个从XML文件构建自己的接口.新构建的界面包含不同的控件,如文本框,标签或按钮.最后一个让我头疼几天,因为每个按钮都会得到自己的命令.
按钮的示例XML代码:
ID="1001" GroupID="1" Control="Button" Content="Apply" Margin="2" Width="60"
Run Code Online (Sandbox Code Playgroud)
这是定义每个按钮的方法:
public Button Button(XElement xElement, Button newButton)
{
if (xElement.Attribute("Width") != null)
newButton.Width = Convert.ToDouble((xElement.Attribute("Width").Value));
if (xElement.Attribute("Content") != null)
{
string sContent = xElement.Attribute("Content").Value;
//Here gets the button its command
Commands Commands = new Commands();
Commands.setCommand(sContent, newButton);
newButton.Content = sContent;
}
return newButton;
}
Run Code Online (Sandbox Code Playgroud)
内容属性同时命名按钮和功能.
问题:基本上我尝试构建一个包含所有可能命令的命令类.每次创建按钮时,该setCommand(sContent, newButton)方法都会扫描commands该类,并通过Switch-Case语句将匹配命令设置为此按钮.
现在,每次单击按钮时,都会触发分配的命令.但这样做甚至可能这样做,还是我走错了路?有没有更简单的解决方案来做到这一点,还是我只是缺少命令绑定的基本要素?
任何提示都表示赞赏.
commandbinding ×11
wpf ×9
mvvm ×5
c# ×4
prism ×2
xaml ×2
binding ×1
listview ×1
mvvm-light ×1
relaycommand ×1
testing ×1
togglebutton ×1
viewmodel ×1