【问题标题】:Is there a way to get the DataContext for a bound Property有没有办法获取绑定属性的 DataContext
【发布时间】:2012-11-15 03:23:50
【问题描述】:

有没有办法在代码中获取下面定义的 ComboBox 的 Text 属性的 DataContent?

    <ComboBox Height="21" Text="{Binding Path=Field1.Value}">
        <ComboBox.Resources>
            <Style TargetType="ComboBox">
                <Setter Property="IsEnabled" Value="False" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Field2.Value}" Value="">
                        <Setter Property="IsEnabled" Value="True" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ComboBox.Resources>
    </ComboBox>

目前 ComboBox 的 DataContext 是它所在的用户控件。这是有道理的,因为我希望我的 Text 绑定到一个属性,而我的 DataTrigger 绑定到另一个属性。但我需要获取绑定到 Text 属性的 DataContext。

【问题讨论】:

    标签: wpf xaml data-binding


    【解决方案1】:

    应该这样做:

    Binding binding = BindingOperations.GetBinding(yourComboBox, ComboBox.TextProperty);
    object theDataContext = binding.Source;
    

    【讨论】:

    • 是的。这是我之前找不到的另一个答案。 link
    猜你喜欢
    • 2010-11-23
    • 1970-01-01
    • 2020-05-01
    • 2014-03-24
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    相关资源
    最近更新 更多