【问题标题】:How to bind a property of a property of a class?如何绑定类的属性的属性?
【发布时间】:2014-01-02 16:44:40
【问题描述】:

我有以下课程:

public class Car
{
    [DataMember(Name = "versionID")]
    public string Id { get; set; }

    [DataMember(Name = "state")]
    public State State { get; set; }

    [DataMember(Name = "type")]
    public string Type { get; set; } 
}


public class State
{
    public string ID { get; set; }
    [DataMember(Name = "on")]
    public bool StateOn{ get; set; }

    [DataMember(Name = "description ")]
    public int Description { get; set; }

}

我有一个 ObservableCollection,我将它作为 ItemsSource 绑定到一个列表框。

列表框项目模板:

<Grid>
    <TextBlock Text="{Binding ID}" />
    <TextBlock Text="{Binding Type}" />
   <ToggleButton Tag="{Binding Description}" IsChecked="{Binding StateOn}"/>

如何将State的StateOn布尔值绑定到ItemTemplate中ToggleButton的IsChecked?

亲切的问候, 尼尔斯

【问题讨论】:

    标签: c# windows-phone-7 binding


    【解决方案1】:

    你应该可以像这样引用它们......

    <Grid>
        <TextBlock Text="{Binding ID}" />
        <TextBlock Text="{Binding Type}" />
       <ToggleButton Tag="{Binding State.Description}" IsChecked="{Binding State.StateOn}"/>
    

    【讨论】:

      【解决方案2】:

      我确信有更简洁的方法可以做到这一点,但您可以尝试在每个控件中设置 DataContext,但在我看来这有点难看:

      <Grid>
          <TextBlock Text="{Binding ID}">
              <TextBlock.DataContext>
                   <local:Car/>
               </TextBlock.DataContext>
          </TextBlock>
          <TextBlock Text="{Binding Type}">
              <TextBlock.DataContext>
                  <local:Car/>
              </TextBlock.DataContext>
          </TextBlock>
          <ToggleButton Tag="{Binding Description}" Content="{Binding StateOn}"
                        IsChecked="{Binding StateOn, Mode=TwoWay}">
              <ToggleButton.DataContext>
                  <local:State/>
              </ToggleButton.DataContext>
          </ToggleButton>
      </Grid>
      

      在你的代码隐藏中:

      public class State : INotifyPropertyChanged
      {
          public event PropertyChangedEventHandler PropertyChanged;
      
          public string ID { get; set; }
          public int Description { get; set; }
      
          private bool stateOn { get; set; }
          public bool StateOn
          {
              get 
              {
                  return stateOn;
              }
      
              set
              {
                  stateOn = value;
                  if (PropertyChanged != null)
                  {
                      PropertyChanged(this, new PropertyChangedEventArgs("StateOn"));
                  }
              }
          }
      }
      

      顺便说一句,我现在删除了这些属性,以免太分散注意力。我还在 ToggleButton 中添加了文本内容,只是为了进行故障排除。如果您不希望在用户单击切换按钮时更改 StateOn 布尔值,您可以将 Mode 更改为 OneWay。更多关于Mode的信息在这里:http://msdn.microsoft.com/en-us/library/system.windows.data.binding.mode(v=vs.110).aspx

      【讨论】:

        猜你喜欢
        • 2010-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多