简体   繁体   中英

How to bind a property of a property of a class?

I have the following class:

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

}

I have a ObservableCollection which I bind to a Listbox as ItemsSource.

ListboxItemTemplate:

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

How can I bind the StateOn bool of State to the IsChecked of the ToggleButton in the ItemTemplate?

Kind regards, Niels

You should just be able to refer to them like so ...

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

I'm sure there are cleaner ways to do this but you can try setting the DataContext within each control but it's kind of ugly, in my opinion:

<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>

And in your code-behind:

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

By the way, I removed the attributes for now so it won't be too distracting. I also added text content to the ToggleButton just for troubleshooting purposes. You can change the Mode to OneWay if you don't want the StateOn bool to change when the user clicks the toggle button. More about Mode here: http://msdn.microsoft.com/en-us/library/system.windows.data.binding.mode(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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