简体   繁体   中英

XAML bind to DependencyProperty instance held in a CLR property

How do I bind to a DependencyProperty instance that is held in a CLR property, from XAML?

I'm trying to generate a 'settings' list (user can modify app. settings via a list of checkboxes).

I would like the list to be dynamically created from the dependency properties in a certain class (MyOptions). This I've achieved, I bind the ListBox to this list (which is a list of DependencyProperty objs)

public IEnumerable<OptionProperty> AvailableOptions
{
    get
    {
         return from  property in GetAttachedProperties(MyOptions) 
                where property.GetMetadata(MyOptions) is OptionPropertyMetaData
                select new OptionProperty { OptionName = property.Name, OptionType = property.PropertyType, OptionDependencyProperty = property };
    }
}

What I need to do is bind the check box in the DataTemplate (for the ListBox) to the DependencyProperty items in the list.

So of course this isn't going to work

<DataTemplate DataType="{x:Type local:OptionProperty}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="30"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" Text="{Binding Path=OptionName}" />
        <CheckBox Grid.Column="1" HorizontalAlignment="Right" IsChecked="{Binding Path=OptionDependencyProperty}"></CheckBox>
    </Grid>   
</DataTemplate>

because it's just binding to the property of OptionProperty called OptionDependencyProperty, instead of the DependencyProperty that is referenced in OptionDependencyProperty.

So how do I bind to a DependencyProperty instance that is held in a CLR property (OptionDependencyProperty), from XAML?

I think my brain-stack is full and can't deal with the abstraction anymore :(

Thanks!

A DependencyProperty is not a value container. It is an identifier which can be used to get/set values for specific instances.

Instead of asking "What is the value of this dependency property?" you want to be asking, "What is the value of this dependency property for that instance?"

You will probably be better off binding to a different property in your OptionProperty class.

Something along the lines of:

public class OptionProperty : INotifyPropertyChanged
{
    public MyOptions MyOptions { get; set; }

    public DependencyProperty OptionDependencyProperty { get; set; }

    public object Value
    {
        get
        {
            return MyOptions.GetValue(OptionDependencyProperty);
        }
        set
        {
            MyOptions.SetValue(OptionDependencyProperty, value);
            RaisePropertyChanged("Value");
        }
    }
    // TODO Implement INotifyPropertyChanged
    // TODO All of the other properties
}

Then you can bind to the Value property, which will get and set the appropriate value for the DependencyProperty on your MyOptions instance.

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