IntelliBox的SelectedItem依赖项属性不会从Datatemplate更新

时间:2017-04-12 10:25:35

标签: c# wpf mvvm data-binding

我正在使用IntelliBox显示自动完成功能。 SelectedItem依赖项属性通过以下方式实现:

public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem", typeof(object), typeof(Intellibox),
    new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnSelectedItemChanged)));

这是在usercontrol(IntelliBox)

中设置新值的方式
private void ChooseCurrentItem() {
    this.SetValue(SelectedItemProperty, ResultsList.SelectedItem);

    _lastTextValue = UpdateSearchBoxText(true);

    OnUserEndedSearchEvent();

    if (Items != null) {
        Items = null;
    }
}

viewmodel中的属性

private Recipient selectedRecipient;

    public Recipient SelectedRecipient
    {
        get
        {
            return selectedRecipient;
        }
        set
        {
            selectedRecipient = value;
            OnPropertyChanged("SelectedRecipient");
        }
    }

如果我在没有datatemplate的情况下使用IntelliBox,则在从依赖属性设置值后调用SelectedRecipient的setter。

<Grid>
    <StackPanel>
        <controls:Intellibox DataProvider="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.SearchProviderRecipients}" 
                                             SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.SelectedRecipient}">
        </controls:Intellibox>
    </StackPanel>
</Grid>

不幸的是,如果IntelliBox在datatemplate中,则设置依赖项属性的值,但它不会在viewmodel中调用setter。

XAML:

<Grid>
    <StackPanel>
        <ListView ItemsSource="{Binding Items}" x:Name="ListViewMain">
            <ListView.ItemTemplate>
                <DataTemplate>
                        <controls:Intellibox DataProvider="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.SearchProviderRecipients}" 
                                             SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.SelectedRecipient}">
                        </controls:Intellibox>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>
</Grid>

我必须做什么才能调用setter?

0 个答案:

没有答案