简体   繁体   English

WPF CheckBox IsChecked绑定问题

[英]WPF CheckBox IsChecked Binding Issue

I have a binding that isn't working and rather than fixing it the only way I know how I would like to better understand why its not working and what options I have other than the one solution I know. 我有一个无法正常工作的绑定,而不是唯一地解决它,我知道我想如何更好地理解它为什么不起作用以及除了我所知道的一个解决方案之外我还有哪些选择。

XAML: XAML:

<CheckBox Grid.Row="6" Grid.Column="1" IsChecked="{Binding Path=CurSerialPort.ExpansionEnabled}" Margin="7"  VerticalAlignment="Stretch" ToolTip="Select whether DHCP is enabled.">
   <CheckBox.Style>
      <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}">
         <Style.Triggers>
            <DataTrigger Binding="{Binding Path=CurSerialPort.ShowExpansionEnable}" Value="False">
                <Setter Property="Visibility" Value="Collapsed" />
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </CheckBox.Style>

Binding of issue: 绑定问题:

IsChecked="{Binding Path=CurSerialPort.ExpansionEnabled}"

DataContext: DataContext:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
     this.DataContext = main.Model;
}

main.Model includes: main.Model包括:

/// <summary>
/// The currently selected serial port
/// </summary>
public SerialModel CurSerialPort
{
   get { return this.curSerialPort; }
   set
   {
      if (value != null)
      {
         this.curSerialPort = value;
      }

      RaisePropertyChanged("CurSerialPort");
   }
}

SerialModel includes: SerialModel包括:

/// <summary>
/// Expansion Enable
/// </summary>
public bool ExpansionEnable
{
   get 
   { 
      return this.expansionEnable; 
   }
   set 
   {
      this.expansionEnable = value; 
   }
}

Being bound as it is isn't working. 被束缚是行不通的。 The set and get of the ExpansionEnable isn't reflecting the check box on the page. ExpansionEnable的设置和获取未反映页面上的复选框。

I know I could just add: 我知道我可以添加:

public bool CurSerialPortExpansionEnable
{
   get { return CurSerialPort.ExpansionEnable; }
   set { CurSerialPort.ExpansionEnable = value; }
}

To the Model and the binding will work because this is how it has been done with properties related to the main model as a whole, although I dont want to have to keep adding single properties like this for every object that we have multiple instances of and would really like to find a solution where the binding is as above. 对模型和绑定将起作用,因为这样做是通过与整个主模型相关的属性来完成的,尽管我不想为我们拥有多个实例的每个对象继续像这样添加单个属性。确实想找到一种绑定如上所述的解决方案。

IsChecked="{Binding Path=CurSerialPort.ExpansionEnabled}"

Edit: Not sure what I changed but this does in fact completely work. 编辑:不确定我更改了什么,但是实际上确实可以正常工作。 Answer below pointed out a flaw of me forgetting the RaisePropertyChanged but other than that all seems good now. 下面的答案指出了我忘记了RaisePropertyChanged的缺点,但除此之外,现在看来一切都很好。

Define isn't working. 定义不起作用。 Are the get set even called? 集合被调用了吗?

Try adding NotifyPropertyChanged to SerialModel 尝试将NotifyPropertyChanged添加到SerialModel

public bool ExpansionEnable
{
   get 
   { 
      return this.expansionEnable; 
   }
   set 
   {
      if(this.expansionEnable == value) return;
      this.expansionEnable = value; 
      RaisePropertyChanged("ExpansionEnable");
   }
}

If above does not work then take out the style and see if that is breaking something. 如果上述方法不起作用,则取出样式,看看是否有破损。
But it looks good to me. 但这对我来说很好。

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

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