WPF CheckBox IsChecked属性绑定问题

时间:2016-01-20 12:15:32

标签: wpf checkbox data-binding

请帮助,我试图做这个小例子。 我的目标是当我保持勾选复选框时,应用程序应该显示我输入的主机的IP地址。但是在视图模型中永远不会更新复选框IsChecked属性,即使它已在UI中更改

我的观点`

    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.Background>
        <LinearGradientBrush>
            <LinearGradientBrush.GradientStops>
                <GradientStop Offset="0.00" Color="LavenderBlush" />
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
    </Grid.Background>
    <StackPanel Grid.Row="0" Margin="150,30,69,236" Grid.ColumnSpan="2">
        <TextBox x:Name="inputBox" Text="{Binding TxtHostName, Mode=TwoWay}"  Foreground="Azure" Background="YellowGreen" VerticalAlignment="Bottom" Height="45"/>

    </StackPanel>
    <Button Command="{Binding StartCommand }" Content="Get IP" HorizontalAlignment="Left" Margin="257,89,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.013,-0.273"/>
    <TextBlock Text="{Binding IpAddress}" Background="BlueViolet" Margin="150,153,69,104" Grid.ColumnSpan="2"  />
    <Button Content="Close" Command="{Binding CloseCommand}" HorizontalAlignment="Left" Margin="257,250,0,0" VerticalAlignment="Top" Width="75"/>
    <CheckBox Content="CheckBox" IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" HorizontalAlignment="Left" Margin="150,111,0,0" VerticalAlignment="Top"/>
</Grid>

`

我的ViewModel:

public class ViewModel:INotifyPropertyChanged
{
    #region INPC
    public void RaisePropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;


    #endregion

    private string txtHostName;

    public string TxtHostName
    {
        get { return txtHostName; }
        set { txtHostName = value;
        RaisePropertyChanged("TxtHostName");
        }
    }

    private string ipAddress;

    public string IpAddress
    {
        get { return ipAddress; }
        set { ipAddress = value;
        RaisePropertyChanged("IpAddress");
        }
    }


    private bool checkbox;

    public bool CheckBox
    {
        get { return checkbox; }
        set { checkbox = value;
        RaisePropertyChanged("IsSelected");
        }
    }

    public event EventHandler RequestClose;

    protected void OnRequestClose()
    {
        if (RequestClose != null)
            RequestClose(this, EventArgs.Empty);
    }

    private RelayCommand _StartCommand;
    public ICommand StartCommand
    {
        get
        {
            if (this._StartCommand == null)
                this._StartCommand = new RelayCommand(StartClick);
            return this._StartCommand;
        }
    }

    private RelayCommand _CloseCommand;
    public ICommand CloseCommand
    {
        get
        {
            if(this._CloseCommand==null)
                this._CloseCommand=new RelayCommand(CloseClick);
            return this._CloseCommand;
        }
    }

    private void CloseClick(object obj)
    {
        OnRequestClose();
    }

    private void StartClick(object obj)
    {
        if (checkbox)
        {
            string HostName = TxtHostName;
            IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);

            foreach (IPAddress ipaddr in ipaddress)
            {
                IpAddress = ipaddr.ToString();
            }

        }

        else
        {
            IpAddress = "Please tick the checkbox";
        }
    }
}

}

RealyCommand应该是它应该的。 CheckBox属性值永远不会改变天气我在UI中更改它。

2 个答案:

答案 0 :(得分:1)

我不确定是否存在复制粘贴错误,但是当您使用标签Checkbox引发属性更改事件时,您的视图模型属性被称为IsSelected

这和绑定中的错误可能是你的问题。根据您的View Model,绑定应该是: -

    <CheckBox Content="CheckBox" IsChecked="{Binding Checkbox, Mode=TwoWay}" HorizontalAlignment="Left" Margin="150,111,0,0" VerticalAlignment="Top"/>

更新:建议您使用C#5.0或更高版本

为了避免在创建setter和引发IPropertyNotifyChange事件时出现拼写错误,我建议使用CallerMemberName属性,如下所示: -

  public void RaisePropertyChanged([CallerMemberName] string propName = "")
  {
      if (this.PropertyChanged != null)
      {
          this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
      }
  }

然后你的例子中的你的二传手变成: -

  private bool checkbox;

  public bool CheckBox
  {
      get { return checkbox; }
      set { checkbox = value;
      RaisePropertyChanged();
    }
  }

当您重构View Model时,编译器将插入调用属性的名称,以确保INotifyProertyChanged事件中的标签与您的属性名称匹配,而无需记住自己手动更新它。

答案 1 :(得分:1)

您针对IsSelected提升了您的属性更改事件,但您的可绑定属性称为Checkbox,将Checkbox重命名为IsSelected并将您的私有变量更新为isSelected。

在这种情况下,Id将变量重命名为IsChecked或ComboBoxIsChecked。