我有一个背景颜色的按钮,我想将此背景颜色作为命令参数发送到命令绑定!我怎样才能做到这一点?
<Button Background="Red" Command="{Binding ChangeColorCommand}" CommandParameter="{Binding this.Background}" />
Run Code Online (Sandbox Code Playgroud) 我试图通过IMultiValueConverter将几个值传递给命令(作为命令参数).这些值在通过转换器时是正确的,但是一旦调用了Can_Execute()和Execute()命令,我就会获得一个空对象数组.有任何想法吗?
XAML:
<Button Content="+" HorizontalAlignment="Right" VerticalAlignment="Top" Width="23" Height="23" Margin="0,0,0,0">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource Converter_MultipleValues}">
<Binding/>
</MultiBinding>
</Button.CommandParameter>
<Button.Command>
<Binding Path="Command_Add_Files" Source="{StaticResource Vm_FileList}"/>
</Button.Command>
</Button>
Run Code Online (Sandbox Code Playgroud)
IMultiValueConverter类:
class cvt_multivalue : IMultiValueConverter {
public object Convert (object[] Values, Type Target_Type, object Parameter, CultureInfo culture) {
if (Target_Type != typeof (object)) throw new NotSupportedException ();
return Values;
}
public object [] ConvertBack (object Value, Type [] Target_Type, object Parameter, CultureInfo culture) {
throw new NotSupportedException ();
}
}
Run Code Online (Sandbox Code Playgroud)
当我没有使用MultiBinding和转换器时,代码工作得很好,但我需要MultiBinding,所以我可以将一些额外的信息传递给命令.
我想(重新)模板控件,例如ComboBox.
XAML:
<ComboBox Style="{StaticResource MyComboBoxStyle}" ... >
<!-- ... -->
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
在ControlTemplate中我想要一个Button.
ResourceDictionary中:
<Style x:Key="MyComboBoxStyle" TargetType="{x:Type ComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid>
<!-- ... -->
<Button Command="{TemplateBinding Tag}" CommandParameter="{Binding ???}" />
<!-- ... -->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
但我想设置CommandParameter应用模板的Control(在本例中为ComboBox).
我怎么解决这个问题?
我没有成功从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) 我正在构建一个具有RibbonWindow和TabCollection的应用程序。
每个RibbonButton都有一个命令来打开特定UserControl的选项卡。每个命令都做相同的事情,只是差别很小,它们打开带有特定UserControl的选项卡。是否有一种很好的方法将UserControl类型传递给一个称为OpenTabCommand的命令?
现在是这样的:
Xaml ...
<RibbonButton Label="OpenTab1"
LargeImageSource="/something.png"
Command="{Binding OpenTab1Command}" />
<RibbonButton Label="OpenTab2"
SmallImageSource="/something.png"
Command="{Binding OpenTab2Command}"/>
Run Code Online (Sandbox Code Playgroud)
...
视图模型
public RelayCommand OpenTab1Command{ get; set; }
public RelayCommand OpenTab2Command { get; set; }
public MainViewModel()
{
OpenTab1Command= new RelayCommand(OpenTab1, param => true);
OpenTab2Command = new SearchCommand(OpenTab2, param => true);
}
private void OpenTab1()
{
var item = new TabItem
{
Content = new Tab1(),
};
TabCollection.Add(item);
item.Focus();
}
private void OpenTab2()
{
var item = new TabItem
{
Content = new Tab2(), …Run Code Online (Sandbox Code Playgroud) EventToCommand 无法在加载事件上传递命令参数
当附加到页面或用户控件的 Load 事件时,EventToCommand 成功调用 ViewModel 中的处理程序,但不传递 CommandParameter。但是,相同的 XAML 附加到另一个事件,例如按钮单击,命令处理程序接收数据绑定数据作为其参数。Xml:
<i:EventTrigger EventName="Loaded" SourceObject="{Binding ElementName=Control}">
<Command:EventToCommand x:Name="etcLoad"
Command="{Binding LoadCommand}"
CommandParameter="{Binding Target, ElementName=Control}" />
</i:EventTrigger>
目标是视图上的字符串 DP。
虚拟机代码:
internal void Load(string p_Param)
{
this.Initialise();
}
public RelayCommand<string> LoadCommand { get; private set; }
Run Code Online (Sandbox Code Playgroud)
并且命令被分配如下:
this.LoadCommand = new RelayCommand<string>(this.Load);
Run Code Online (Sandbox Code Playgroud)
我几乎可以肯定,问题在于绑定晚于分配给目标 DP 或类似的东西。我有兴趣为此尽快找到解决方案,或者以其他方式从 View 中获取字符串并进入 ViewModel,其中从 OnNavigateTo 覆盖分配字符串。目标是根据通过 URI 提供的查询属性提供选项卡的选择,即“/Views/DisplayTabDetails?Tab=Tab1”或类似的。
我正在使用MvmmCross v3框架和Windows.UI.Interactivity开发 WinRT .
我想在TextChanged事件上使用带有EventTrigger的TextBox来启动Command.而且,我想在CommandParameter中使用textBox的文本.
所以我有这个代码
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding UpdateText}" CommandParameter="{Binding Text}"/>
</i:EventTrigger>
public ICommand UpdateText
{
get
{
return new MvxCommand<object>((textSearch) =>
{
// code...
});
}
}
Run Code Online (Sandbox Code Playgroud)
但我的textSearch参数等于"{Windows.UI.Xaml.Controls.TextChangedEventArgs}",所有这些属性都为NULL.
我试图在绑定中明确声明我的ElementName
CommandParameter="{Binding Path=Text, ElementName=tes}
Run Code Online (Sandbox Code Playgroud)
但它也失败了.
谢谢
xaml eventtrigger commandparameter windows-runtime mvvmcross
因此,我从xaml中找到了这个答案Pass命令参数,我认为这是我了解的大多数方法。我遇到的问题是,当我在数据网格中选择一行时,它会触发命令,但所选项目为null。
我不知道并且怀疑是问题所在,我应该将所选项目传递给哪种类型的视图模型?目前,我正在使用IList,如我的viewmodel代码所示:
namespace Project_Manager.ViewModel
{
public class ProjectSummaryViewModel : ObservableObject
{
public ProjectSummaryViewModel()
{
ProjectSummary = DatabaseFunctions.getProjectSummaryData();
}
private ObservableCollection<ProjectSummaryModel> projectsummary;
public ObservableCollection<ProjectSummaryModel> ProjectSummary
{
get { return projectsummary; }
set
{
projectsummary = value;
OnPropertyChanged("ProjectSummary");
}
}
public ICommand DeleteRowCommand
{
get { return new ParamDelegateCommand<IList<ProjectSummaryModel>>(DeleteRow); }
}
private void DeleteRow(IList<ProjectSummaryModel> projectsummaryselected)
{
string name = projectsummaryselected[0].ProjectName;
}
}
}
Run Code Online (Sandbox Code Playgroud)
数据网格的XAML视图代码如下所示:
<Window x:Class="Project_Manager.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">
<!--<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>-->
<Grid>
<Grid.RowDefinitions> …Run Code Online (Sandbox Code Playgroud) wpf ×6
xaml ×3
binding ×2
c# ×1
command ×1
datagrid ×1
eventtrigger ×1
icommand ×1
listview ×1
load ×1
multibinding ×1
mvvm ×1
mvvm-light ×1
mvvmcross ×1