سؤال

ولدي على UserControl بسيط يحتوي على التسمية، تحرير وسرد وزر. وباختصار، فإنه لاستخدامها في جميع تقريبا من بلدي المشاهدات عدة مرات، في كل مرة المتوفرة مع ItemsSource مختلفة وCreateItemCommand باستخدام الربط إلى خصائص ViewModel بلدي.

والتسمية وتحرير وسرد جزء من UserControl آخر (LabeledComboBox) الذي يعمل على ما يرام.

والمشكلة هي أنه عندما أحاول ربط الأمر في الإطار الذي يحتوي بلدي UserControl أحصل على استثناء التالية:

<اقتباس فقرة>   

وA 'ربط' لا يمكن تعيين على ممتلكات 'CreateItemCommand' من نوع 'MutableComboBox. A 'الربط' لا يمكن إلا أن توضع على DependencyProperty من DependencyObject.

وهنا هو XAML لMutableComboBox:

<UserControl x:Class="Albo.Presentation.Templates.MutableComboBox"
x:Name="MCB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:Albo.Presentation.Templates" >
<StackPanel Height="25" Orientation="Horizontal">
    <uc:LabeledComboBox x:Name="ComboBoxControl"
        Label = "{Binding ElementName=MCB, Path=Label}"
        ItemsSource="{Binding ElementName=MCB, Path=ItemsSource}"
        SelectedItem="{Binding ElementName=MCB, Path=SelectedItem}" />
    <Button x:Name="CreateItemButton"
        Grid.Column="1" Width="25" Margin="2,0,0,0"
        Content="+" FontFamily="Courier" FontSize="18" 
        VerticalContentAlignment="Center"
        Command="{Binding ElementName=MCB, Path=CreateItemCommand}"/>
</StackPanel>
</UserControl>

وهنا هو codebehind له:

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

    public string Label
    {
        get { return this.ComboBoxControl.Label; }
        set { this.ComboBoxControl.Label = value; }
    }

    #region ItemsSource dependency property
    public static readonly DependencyProperty ItemsSourceProperty =
        ItemsControl.ItemsSourceProperty.AddOwner(
            typeof(MutableComboBox),
            new PropertyMetadata(MutableComboBox.ItemsSourcePropertyChangedCallback)
        );

    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public static void ItemsSourcePropertyChangedCallback(
        DependencyObject controlInstance,
        DependencyPropertyChangedEventArgs e)
    {
        MutableComboBox myInstance = (MutableComboBox)controlInstance;

        myInstance.ComboBoxControl.ItemsSource = (IEnumerable)e.NewValue;
    } 
    #endregion // ItemsSource dependency property

    #region SelectedItem dependency property
    // It has just the same logic as ItemsSource DP.
    #endregion SelectedItem dependency property

    #region CreateItemCommand dependency property

    public static readonly DependencyProperty CreateItemCommandProperty =
        DependencyProperty.Register(
            "MutableComboBoxCreateItemCommandProperty",
            typeof(ICommand),
            typeof(MutableComboBox)
        );

    public ICommand CreateItemCommand
    {
        get { return (ICommand)GetValue(CreateItemCommandProperty); }
        set { SetValue(CreateItemCommandProperty,value); }
    }

    #endregion // CreateItem dependency property
}

وكما ترون، وأنا استخدم نهجين مختلفين لتسجيل المرحلين بلدي: يؤخذ ItemsSource من ItemsControl موانئ دبي ويتم إنشاء CreateItemCommand التي كتبها DependencyProperty.Register (...). حاولت استخدام Button.CommandProperty.AddOwner (...) ولكن كان الاستثناء ذاته.

وهنا هو كيف لي أن محاولة الربط:

<Window ...
    xmlns:uc="clr-namespace:Albo.Presentation.Templates">
    <uc:MutableComboBox Label="Combo" 
        ItemsSource="{Binding Path=Recipients}"
        CreateItemCommand="{Binding Path=CreateNewRecipient}"/>
</Window>

ويتم تعيين DataContext من النافذة لViewModel الذي يوفر ObservableCollection من المستلمين وICommand CreateNewRecipient كخصائص بسيطة المناسبة.

وماذا أفعل الخطأ؟ الشيء الوحيد الذي أريده في هذه الحالة بالذات هو لفضح خاصية Button.Command للاستخدام خارج بلدي UserControl، تماما كما ItemsSource. أحاول أن استخدام الأوامر بطريقة خاطئة؟ كيف يمكنني ربط أوامر UserControls بلدي من الضوابط أو نوافذ أخرى؟

وتأملوا معي على newb مع هذه الاشياء القيادة وDependencyProperty. سيكون موضع تقدير أي مساعدة. غوغلد طوال الليل ولم تجد أي شيء قابل للاستخدام في حالتي. العفو لغتي الإنجليزية.

هل كانت مفيدة؟

المحلول

ولقد سجلت اسم خاطئ لممتلكات اعتمادك. يجب أن تكون:

public static readonly DependencyProperty CreateItemCommandProperty =
        DependencyProperty.Register(
            "CreateItemCommand",
            typeof(ICommand),
            typeof(MutableComboBox)
        );

لاحظ السلسلة "CreateItemCommand". يجب عليك قراءة من خلال هذه الوثائق MSDN للحصول على معلومات مفصلة عن اتفاقيات ل خصائص التبعية.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top