UpdateSourceTrigger按钮单击转换器 [英] UpdateSourceTrigger button click Converter
本文介绍了UpdateSourceTrigger按钮单击转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用以下代码,在更新/更改DependencyProperties之一时,将调用Convert方法。
With the following code, the Convert method is being called when one of the DependencyProperties are being updated/changed.
我希望仅在单击按钮时才调用转换器。
我该怎么做?
I want the converter to be called only when the button is clicked. How can I do that?
这是代码:
<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>
推荐答案
我认为将纠正以下代码:
I think will correct something like this code:
xaml
<Button Content="Create Project" Click="Button_Click"/>
cs
private void Button_Click(object sender, RoutedEventArgs e)
{
string param1 = txtDesc.Text;
string param2 = txtName.Text;
object param3 = ListBox.SelectedItem;
object param4 = TreeView.SelectedItem;
object convertResult = MyConverterUtils.Convert(param1, param2, param3, param4);
(sender as Button).CommandParameter = convertResult;
//or you can execute some method here instead of set CommandParameter
}
public class MyConverterUtils
{
//convert method
//replace Object on your specific types
public static Object Convert(string param1, string param2,Object param3,Object param4)
{
Object convertResult=null;
//convert logic for params and set convertResult
//for example convertResult = (param1 + param2).Trim();
return convertResult;
}
}
这篇关于UpdateSourceTrigger按钮单击转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文