一般绑定都是从数据源或控件属性来绑定,但我想绑定的是:
在窗体代码中定义一个变量,而在xaml中去绑定它,求人给一个正解。
比如后台用hasedited变量来保存页面是否已经修改过,从而改变“保存”按钮是否可用
简码:
xaml部分
=<Window x:Class="数据绑定.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Margin="0,0,359,281" IsEnabled="{Binding  Path=CanEdit}">
            目标按钮
        </Button>
        
        <Button Margin="12,66,359,216" Click="Button_Click">设置</Button>
    </Grid>
</Window>
cs部分
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }        bool CanEdit = false;        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CanEdit = !CanEdit;
        }
    }wpf数据binding

解决方案 »

  1.   

    完全没有必要这样做。
    因为MVVMLight的RelayCommand命令是自动判断命令是否有效并会将相应的按钮自变灰。既然你都用WPF了,就不要用Winform那套搞法了。
      

  2.   

    再说,你都舍得写后台代码了,抛弃了MVVM模式,那就还不如直接在后台代码里处理Buttn的isEnabled属性。
    一边又要绑定,一边要写后台代码,搞个不伦不类。
      

  3.   

    只是暂时要用下,还没有学习他的 MVVM模式,还是先告诉我怎么像那样绑吧,
      

  4.   

    好吧:
    public partial class MainWindow : Window
    {
    public bool CanEdit
    {
    get { return (bool)GetValue(CanEditProperty); }
    set { SetValue(CanEditProperty, value); }
    } public DependencyProperty CanEditProperty = DependencyProperty.Register("CanEdit", typeof(bool), typeof(MainWindow)); public MainWindow()
    {
    InitializeComponent();
    this.DataContext = this;
    } private void Button_Click(object sender, RoutedEventArgs e)
    {
    CanEdit = !CanEdit;

    }
    }