Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新资源记录器按钮单击转换器_C#_Wpf_Converter - Fatal编程技术网

C# 更新资源记录器按钮单击转换器

C# 更新资源记录器按钮单击转换器,c#,wpf,converter,C#,Wpf,Converter,使用以下代码,当更新/更改其中一个DependencyProperties时,将调用Convert方法 我希望仅在单击按钮时调用转换器。 我该怎么做 代码如下: <Button Content="Create Project"> <Button.CommandParameter> <MultiBinding Converter="{StaticResource MyConverter}" U

使用以下代码,当更新/更改其中一个DependencyProperties时,将调用Convert方法

我希望仅在单击按钮时调用转换器。 我该怎么做

代码如下:

 <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.
        }
    }
使用它可以非常简单:

  • 使用在MVVM中声明一个
    ICommand
  • 将按钮的
    命令
    属性绑定到声明的命令
  • 在命令的Execute方法中执行您的逻辑
  • 更新所需的属性
  • 视图模型

     <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您可以发布它作为我的特定代码的答案吗?谢谢您,很抱歉最近的评论。