当前位置: 代码迷 >> C# >> wpf在代码中如何给CommandParameter设置Binding
  详细解决方案

wpf在代码中如何给CommandParameter设置Binding

热度:82   发布时间:2016-05-05 02:38:16.0
wpf在代码中怎么给CommandParameter设置Binding?


button1.CommandParameter = new Binding() { Path = new PropertyPath("Text"), Source = textbox };

void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show(e.Parameter.ToString());
        }


上面是我写的给CommandParameter设置Binding的代码,为什么没能达到效果?XAML中只要CommandParameter="{Binding ElementName=textbox,Path=Text}"就可以了。
------解决思路----------------------

            button1.SetBinding(Button.CommandParameterProperty,
                new Binding() { Source = textbox, Path = new PropertyPath("Text") });
  相关解决方案