문제

간단한 Usercontrol 포함 라벨, 콤보 박스 및 버튼이 있습니다. 간단히 말해서, 그것은 거의 모든 뷰에서 여러 번 사용되어야하며, 매번 내 뷰 모델 속성에 바인딩을 사용하여 다른 항목 소스 및 CreateItemCommand와 함께 제공됩니다.

레이블과 콤보 박스는 다른 USERCONTROL (LADEDEDCOMBOBOX)의 일부로 잘 작동합니다.

문제는 USERCONTROL이 포함 된 창에서 명령을 바인딩하려고 할 때 다음과 같은 예외를 얻는다는 것입니다.

'MutableComboBox'유형의 'CreateItemCommand'속성에 '바인딩'을 설정할 수 없습니다. '바인딩'은 종속성 요법의 종속성 요법에만 설정할 수 있습니다.

MutableCombobox의 XAML은 다음과 같습니다.

<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>

다음은 다음과 같습니다.

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
}

보시다시피, 나는 DPS 등록에 두 가지 다른 접근법을 사용합니다. 항목 소스는 itemscontrol dp에서 가져 왔으며 CreateItemCommand는 fectencyProperty.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>

창의 데이터 컨텍스트는 적절한 뷰 모델로 설정되어 수신자와 Icommand CreateNewrecipient의 관측형 수집을 간단한 특성으로 제공합니다.

내가 뭘 잘못하고 있죠? 이 특별한 경우에 내가 원하는 유일한 것은 itemssource와 마찬가지로 USERCONTROL 이외의 사용을 위해 Button.Command 속성을 노출시키는 것입니다. 명령을 잘못 사용하려고합니까? 다른 컨트롤이나 창에서 USERCONTROL의 명령에 어떻게 바인딩 할 수 있습니까?

이 명령과 종속성 장애물을 사용하여 새로운 새로운 것을 고려하십시오. 모든 도움이 감사하겠습니다. 밤새 Google을 구글링하고 제 경우에는 사용할 수있는 것을 찾지 못했습니다. 내 영어 용서.

도움이 되었습니까?

해결책

의존성 속성에 대한 잘못된 이름을 등록했습니다. 그것은해야한다:

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

문자열은 "CreateItemCommand"입니다. 당신은 읽어야합니다 이 MSDN 문서 의존성 속성에 대한 규칙에 대한 자세한 정보.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top