UpdateSourceTrigger button click Converter

断了今生、忘了曾经 提交于 2019-12-25 04:55:12

问题


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?

Here's the code:

 <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>

回答1:


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;
         } 
    }



回答2:


With MVVM it can be very easy:

  1. Declare a ICommand in your MVVM using RelayCommand.
  2. Bind the Button's Command property to the declared command.
  3. Do your logic in the Command's Execute method.
  4. Update the required properties.

ViewModel

    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}"/>


来源:https://stackoverflow.com/questions/14478931/updatesourcetrigger-button-click-converter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!