WPF:从用户控件绑定属性到ViewModel

时间:2015-01-08 20:12:59

标签: c# wpf xaml mvvm user-controls

我有一个名为AntecedentControl的UserControl:

XAML:

<UserControl x:Name="AntecedentUserControl">

<ScrollViewer DataContext="{Binding ElementName=AntecedentUserControl}">
    <telerik:RadGridView ItemsSource="{Binding Path=AntecedentList}"
                         AutoGenerateColumns="False"

        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Section, Mode=TwoWay}"
                                        Header="Seccion"
                                        IsVisible="True"
                                        IsFilterable="False"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Group, Mode=TwoWay}"
                                    Header="Grupo"
                                    IsVisible="True"
                                    IsFilterable="False"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Tome, Mode=TwoWay}"
                                    Header="Tomo"
                                    IsVisible="True"
                                    IsFilterable="False"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Volume, Mode=TwoWay}"
                                    Header="Volumen"
                                    IsVisible="True"
                                    IsFilterable="False"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Inscription, Mode=TwoWay}"
                                    Header="Inscripcion"
                                    IsVisible="True"
                                    IsFilterable="False"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Bis, Mode=TwoWay}"
                                    Header="Bis"
                                    IsVisible="True"
                                    IsFilterable="False"/>
            <telerik:GridViewColumn>
                <telerik:GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <telerik:RadButton Content="Delete"
                                           Command="telerikGrid:RadGridViewCommands.Delete"
                                           CommandParameter="{Binding}"/>
                    </DataTemplate>
                </telerik:GridViewColumn.CellTemplate>
            </telerik:GridViewColumn>
        </telerik:RadGridView.Columns>

    </telerik:RadGridView>
</ScrollViewer>

代码隐藏(C#):

public partial class AntecedentControl : UserControl
{
    public ObservableCollection<Antecedent> AntecedentList
    {
        get { return (ObservableCollection<Antecedent>)GetValue(AntecedentListProperty); }
        set { SetValue(AntecedentListProperty, value); }
    }

    public static readonly DependencyProperty AntecedentListProperty =
        DependencyProperty.Register("AntecedentList",
        typeof(ObservableCollection<Antecedent>),
        typeof(AntecedentControl),
        new FrameworkPropertyMetadata(new ObservableCollection<Antecedent>(), OnListPropertyChanged));

    private static void OnListPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        var antecedentList = source as AntecedentControl;
        var value = e.NewValue as ObservableCollection<Antecedent>;
        antecedentList.AntecedentList = value;
    }

    public AntecedentControl()
    {
        InitializeComponent();
    }
}

而且,在另一个视图中,我正在调用此UserControl,如下所示:

<DomainTransmissionControls:AntecedentControl AntecedentList="{Binding ElementName=AntecedentUserControl, Path=Antecedents, Mode=TwoWay}"/>

在这个单独视图的ViewModel上,我有一个名为'Antecedents'的属性:

public ObservableCollection<Antecedent> Antecedents { get; set; }

简单来说:AntecedentControl UC是一种网格,有一种方法可以在所述网格上添加/编辑项目。当前UC的属性“AntecedentList”是列表被“保存”的位置。

正在调用'AntecedentControl'的其他UC的ViewModel的属性'Antecedents'应绑定到AntecedentControl UC的'AntecedentList'。

我想知道如何将ViewModel上的此属性绑定到单独的UserControl(AntecedentControl)中找到的属性'AntecedentList'属性,因为此当前代码返回null。

我认为AntecedentControl UC上的DependencyProperty声明有问题,因为在'OnListPropertyChanged'方法上设置断点会显示e.NewValue返回null。

如果是这样,为什么不返回Dependency Property语句中声明的默认值?

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

您的绑定似乎不正确。您似乎正在尝试访问您创建的UserControl中的元素(AntecedentUserControl)?

除非你为了简单而省略了一些代码,否则你似乎甚至不需要DependencyProperty?你有什么理由不能设置DataContext吗?

<强> SomeView.xaml

<DomainTransmissionControls:AntecedentControl DataContext="{Binding Antecedents}" />

<强> SomeViewModel.cs

private ObservableCollection<Antecedent> _antecedents;

public ObservableCollection<Antecedent> Antecedents
{
    get
    {
        if (_antecedents == null)
            _antecedents = new ObservableCollection<Antecedent>();

        return _antecedents;
    }
}

<强> AntecedentControl.xaml

<UserControl>
    <ScrollViewer>
       <telerik:RadGridView ItemsSource="{Binding}" AutoGenerateColumns="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Section, Mode=TwoWay}"
                                            Header="Seccion" IsFilterable="False" IsVisible="True" />
                <!-- Additional column definitions. -->
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </ScrollViewer>
</UserControl>

<强> AntecedentControl.xaml.cs

public partial class AntecedentControl : UserControl
{
    public AntecedentControl()
    {
        InitializeComponent();
    }
}