简体   繁体   English

C#WPF绑定不适用于标签

[英]C# Wpf binding not working for label

I am trying to change the content of a label when a button is clicked but it doesn't work. 我试图在单击按钮时更改标签的内容,但是它不起作用。 If I use the same code to change the Content of the button I click on it works. 如果我使用相同的代码来更改按钮的内容,则单击它即可。

This is my code: 这是我的代码:

<Window x:Class="TestGrila.Grila"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:view="clr-namespace:TestGrila.View"  
    xmlns:viewModel="clr-namespace:TestGrila.ViewModel"
     xmlns:res="clr-namespace:TestGrila.Resources"
     xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    WindowStartupLocation="CenterScreen"
    Height="350" Width="532.463" Icon="Resources/icon.png">
<Window.DataContext>
    <viewModel:IntrebariViewModel />
</Window.DataContext>

<Grid>
    <Label 
      Content="{Binding Response,UpdateSourceTrigger=PropertyChanged}"
      HorizontalAlignment="Left" VerticalAlignment="Top" Margin="50,55,0,0"/>


    <Button
         Grid.Row="0"
         Command="{Binding MyButtonClickCommand}"
         Content="{x:Static res:Strings.Container_Button_MoveBack}" Margin="246,0,189,27" RenderTransformOrigin="0.054,0.495" Height="30" VerticalAlignment="Bottom" >
         <Button.DataContext>
            <viewModel:IntrebariViewModel/>
         </Button.DataContext>
    </Button>

    <Button
       Grid.Row="0"
       Command="{Binding MoveNextCommand}"
       Content="{x:Static res:Strings.Container_Button_MoveNext}" Height="30" VerticalAlignment="Bottom" Margin="361,0,75,27" >
       <Button.DataContext>
          <viewModel:IntrebariViewModel/>
       </Button.DataContext>
    </Button>   

</Grid>

This is the code form the View Model: 这是视图模型的代码:

 public class IntrebariViewModel:INotifyPropertyChanged
{

    string response;

    public string Response {

        get  { return this.response; }

        set {
            if (this.response == value)
                return;

            this.response = value;
            NotifyPropertyChanged("Response");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }


    public ICommand MyButtonClickCommand
    {
        get { return new DelegateCommand<object>(FuncToCall); }
    }

    private void FuncToCall(object context)
    {

        Response = "New content"; 

    }
  }

You are creating 3 separate view model instances. 您正在创建3个单独的视图模型实例。 You should remove the extra DataContext lines from your xaml: 您应该从xaml中删除多余的DataContext行:

<Button
     Grid.Row="0"
     Command="{Binding MyButtonClickCommand}"
     Content="{x:Static res:Strings.Container_Button_MoveBack}" Margin="246,0,189,27" RenderTransformOrigin="0.054,0.495" Height="30" VerticalAlignment="Bottom" >
     <!-- Don't include this!!!!
       <Button.DataContext>
         <viewModel:IntrebariViewModel/>
       </Button.DataContext>
     -->
</Button>

Those cause your buttons to have their own view models, and not work on the main one for the window. 这些会导致您的按钮具有自己的视图模型,而不能在窗口的主视图模型上使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM