【问题标题】:WPF CheckBox IsChecked Property binding issuesWPF CheckBox IsChecked 属性绑定问题
【发布时间】:2016-04-26 06:52:57
【问题描述】:

请帮忙,我正在尝试做这个小例子。 我的目标是当我选中复选框时,应用程序应该显示我输入的主机的 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>

`

我的视图模型:

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 中更改它。

【问题讨论】:

  • 是否需要将相对源设置为 templatedParent ?
  • 我删除了 RelativeSource={RelativeSource TemplatedParent} 并将 ViewModel Bool 变量名称更改为 IsSelected。现在它起作用了。谢谢。

标签: wpf checkbox data-binding


【解决方案1】:

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

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

【讨论】:

  • 谢谢。我将 bool 变量重命名为 IsSelected,它现在可以工作了。并在 xaml 中删除了 theRelativeSource={RelativeSource TemplatedParent}。
【解决方案2】:

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

这和绑定中的错误可能是您的问题。根据您的视图模型,绑定应该是:-

    <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();
    }
  }

意味着当您重构视图模型时,编译器将插入调用属性的名称,以确保 INotifyProertyChanged 事件中的标签与您的属性名称匹配,而您不必记住自己手动更新它。

【讨论】:

  • 我使用的是 c# 4.5。感谢您的输入。问题解决了。
  • 如果您使用的是 .NET Framework 4.5(一个是编译器版本,另一个是库版本号),您可能会使用 C# 5.0,所以我的建议适用于您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-25
  • 2015-07-24
  • 1970-01-01
  • 2016-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多