WPF - Getting a property value from a binding path

前端 未结 4 1338
半阙折子戏
半阙折子戏 2020-12-15 22:57

if I have an object say called MyObject, which has a property called MyChild, which itself has a property called Name. How can I get the value of that Name property if all

相关标签:
4条回答
  • 2020-12-15 23:30

    I found a way to do this, but it's quite ugly and probably not very fast... Basically, the idea is to create a binding with the given path and apply it to a property of a dependency object. That way, the binding does all the work of retrieving the value:

    public static class PropertyPathHelper
    {
        public static object GetValue(object obj, string propertyPath)
        {
            Binding binding = new Binding(propertyPath);
            binding.Mode = BindingMode.OneTime;
            binding.Source = obj;
            BindingOperations.SetBinding(_dummy, Dummy.ValueProperty, binding);
            return _dummy.GetValue(Dummy.ValueProperty);
        }
    
        private static readonly Dummy _dummy = new Dummy();
    
        private class Dummy : DependencyObject
        {
            public static readonly DependencyProperty ValueProperty =
                DependencyProperty.Register("Value", typeof(object), typeof(Dummy), new UIPropertyMetadata(null));
        }
    }
    
    0 讨论(0)
  • 2020-12-15 23:31

    I am doing it this way. Please let me know if this is a terrible idea, as C# is just a side job for me so I am not an expert objectToAddTo is of type ItemsControl:

    BindingExpression itemsSourceExpression = GetaBindingExression(objectToAddTo);
    object itemsSourceObject = (object)itemsSourceExpression.ResolvedSource;
    string itemSourceProperty = itemsSourceExpression.ResolvedSourcePropertyName;
    
    object propertyValue = itemsSourceObject.GetType().GetProperty(itemSourceProperty).GetGetMethod().Invoke(itemsSourceObject, null); // Get the value of the property
    
    0 讨论(0)
  • 2020-12-15 23:34

    not sure what you want to do but and how (xaml or code) yet you can always name your object

    <MyObject x:Name="myBindingObject" ... />
    

    an then use it in code

    myBindingObject.Something.Name
    

    or in xaml

    <BeginStoryboard>
     <Storyboard>
        <DoubleAnimation
            Storyboard.TargetName="myBindingObject"
            Storyboard.TargetProperty="Background"
            To="AA2343434" Duration="0:0:2" >
        </DoubleAnimation>
     </Storyboard>
    </BeginStoryboard>
    
    0 讨论(0)
  • 2020-12-15 23:55

    I developed a nuget package Pather.CSharp that does exactly what you need.

    It contains a class Resolver that has a Resolve method which behaves like @ThomasLevesque's GetValue method.
    Example:

    IResolver resolver = new Resolver(); 
    var o = new { Property1 = Property2 = "value" } }; 
    var path = "Property1.Property2";    
    object result = r.Resolve(o, path); //the result is the string "value"
    

    It even supports collection access via index or dictionary access via key.
    Example paths for these are:

    "ArrayProperty[5]"
    "DictionaryProperty[Key]"
    
    0 讨论(0)
提交回复
热议问题