C# 更新资源记录器按钮单击转换器
使用以下代码,当更新/更改其中一个DependencyProperties时,将调用Convert方法 我希望仅在单击按钮时调用转换器。 我该怎么做 代码如下:C# 更新资源记录器按钮单击转换器,c#,wpf,converter,C#,Wpf,Converter,使用以下代码,当更新/更改其中一个DependencyProperties时,将调用Convert方法 我希望仅在单击按钮时调用转换器。 我该怎么做 代码如下: <Button Content="Create Project"> <Button.CommandParameter> <MultiBinding Converter="{StaticResource MyConverter}" U
<Button Content="Create Project">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource MyConverter}" UpdateSourceTrigger="Explicit" Mode="TwoWay">
<Binding Path="Text" ElementName="txtDesc"/>
<Binding Path="Text" ElementName="txtName"/>
<Binding Path="SelectedItem" ElementName="ListBox"/>
<Binding Path="SelectedItem.Language" ElementName="TreeView"/>
</MultiBinding>
</Button.CommandParameter>
</Button>
我认为这样的代码可以纠正: xaml
<Button Content="Create Project" Click="Button_Click"/>
public class MyViewModel : INotifyPropertyChanged
{
public ICommand UpdateSomething { get; private set; }
public MyViewModel()
{
UpdateSomething = new RelayCommand(MyCommandExecute, true);
}
private void MyCommandExecute(object parameter)
{
// Your logic here, for example using your converter if
// you really need it.
// At the end, update the properties you need
// For example adding a item to an ObservableCollection.
}
}
使用它可以非常简单:
ICommand
命令
属性绑定到声明的命令 <Button Content="Create Project" Click="Button_Click"/>
public class MyViewModel : INotifyPropertyChanged
{
public ICommand UpdateSomething { get; private set; }
public MyViewModel()
{
UpdateSomething = new RelayCommand(MyCommandExecute, true);
}
private void MyCommandExecute(object parameter)
{
// Your logic here, for example using your converter if
// you really need it.
// At the end, update the properties you need
// For example adding a item to an ObservableCollection.
}
}
XAML
<Button Content="Create Project" Command="{Binding UpdateSomething}"/>
可能更容易为该按钮编写单击处理程序?xaml:cs:IMultiValueConverter multiValueConverter=new MyConverter();对象[]值=新对象[4];值[1]=txtDesxr.Text。。。对象convResult=多值转换器.Convert(值,…)@Frank59您应该将其作为答案与代码示例一起发布。我认为它应该可以正常工作,因为按钮。单击在按钮之前运行。Command
,因此您应该能够使用它来设置按钮。CommandParameter
@Frank59您可以发布它作为我的特定代码的答案吗?谢谢您,很抱歉最近的评论。