检查数据绑定是否存在

时间:2013-02-27 16:09:24

标签: c# wpf binding

我正在将xaml文件动态加载到具有绑定的程序中:

<ListView
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Grid.Row="2" BorderBrush="White" Name="ListView1"
    ItemsSource="{Binding Path=line}" HorizontalAlignment="Stretch">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Lines"
             DisplayMemberBinding="{Binding Path=aline}" />
        </GridView>
    </ListView.View>
</ListView >

在我的程序中,我想检查Binding是否存在。

应该如何实现?

修改alineDataContext对象的属性

2 个答案:

答案 0 :(得分:11)

您可以检查这样的绑定:

BindingExpression be = BindingOperations.GetBindingExpression(ListView1, ItemsSourceProperty);
return be != null ? "ItemsSource is bound" : "ItemsSource is not bound";

答案 1 :(得分:-3)

if (ListView1.ItemsSource != null)
            Console.WriteLine("Is Bound");
        else Console.WriteLine("Is Not bound");