绑定到自定义控件的一部分中的事件

时间:2017-01-22 08:51:48

标签: c# wpf xaml wpf-controls

我试图在以下位置实施该示例:

https://github.com/olohmann/WpfRxControls

自定义控件有三个部分:

PART_TextBox

PART_Popup

PART_ListBox

相关来源:

https://github.com/olohmann/WpfRxControls/blob/master/WpfRxControls/AutoCompleteTextBox.cs

https://github.com/olohmann/WpfRxControls/blob/master/WpfRxControls/Themes/Generic.xaml

所有部分都已到位,使用新控件的代码如下:

<ctrls:AutoCompleteTextBox
            Grid.Row="1"
            AutoCompleteQueryResultProvider="{Binding AutoCompleteQueryResultProvider}"
            Margin="10" FontSize="20" PopupHeight="300">

</ctrls:AutoCompleteTextBox>

我只需要在我的页面XAML / ViewModel中挂钩ListBox的SelectionChanged事件,如何实现?

编辑:在XAML / VM中,不要查看后面的代码。到目前为止,所有视图代码背后都是空的,我希望保持这种方式。

我认为有一些方法可以在MainWindow.XAML的ControlTemplate覆盖中覆盖PART_ListBox?

编辑:最终解决方案,感谢mm8

在AutoCompleteTextBox.cs中,创建ICommand类型的依赖项属性:

public const string AutoCompleteSelectionChangedPropertyName = "AutoCompleteSelectionChangedCommand";

        public ICommand AutoCompleteSelectionChangedCommand
        {
            get { return (ICommand) GetValue(AutoCompleteSelectionChangedProperty); }
            set { SetValue(AutoCompleteSelectionChangedProperty, value);}
        }

        public static readonly DependencyProperty AutoCompleteSelectionChangedProperty = DependencyProperty.Register(
            AutoCompleteSelectionChangedPropertyName,
            typeof(ICommand),
            typeof(AutoCompleteTextBox));

在SetResultText方法中:

AutoCompleteSelectionChangedCommand?.Execute(autoCompleteQueryResult);

查看/ ViewModel用法:

<ac:AutoCompleteTextBox Name="AutoComplete" 
    AutoCompleteQueryResultProvider="{Binding AutoCompleteQueryResultProvider}" 
    FontSize="12"
    AutoCompleteSelectionChangedCommand="{Binding CommandEditValueChanged}">
</ac:AutoCompleteTextBox>


public ICommand CommandEditValueChanged { get; set; }

public MainWindowViewModel(){
     CommandEditValueChanged = new DelegateCommand<object>(OnEditValueChanged);
}

private void OnEditValueChanged(object result){
     // do stuff
}

1 个答案:

答案 0 :(得分:2)

您可以在视图中处理AutoCompleteTextBox的Loaded事件,使用FindName方法获取对控件模板中PART_ListBox的引用,然后为ListBox的SelectionChanged事件挂钩事件处理程序:

<ctrls:AutoCompleteTextBox
            Grid.Row="1"
            AutoCompleteQueryResultProvider="{Binding AutoCompleteQueryResultProvider}"
            Margin="10" FontSize="20" PopupHeight="300" Loaded="AutoCompleteTextBox_Loaded">

</ctrls:AutoCompleteTextBox>
private void AutoCompleteTextBox_Loaded(object sender, RoutedEventArgs e)
{
    AutoCompleteTextBox actb = sender as AutoCompleteTextBox;
    ListBox lb = actb.Template.FindName("PART_ListBox", actb) as ListBox;
    if (lb != null)
    {
        lb.SelectionChanged += (ss, ee) =>
        {
            MainWindowViewModel vm = DataContext as MainWindowViewModel;
            //invoke a command of the view model or do whatever you want here...
            var selectedItem = lb.SelectedItem;
        };
    }
}

您的视图模型类没有(也不应该有)参考,也没有关于作为控件模板一部分的ListBox的知识。

  

我认为有一些方法可以在MainWindow.XAML的ControlTemplate覆盖中覆盖PART_ListBox?

然后你必须覆盖/重新定义 AutoCompleteTextBox控件的整个 ControlTemplate,这似乎有点不必要。

MVVM 关于从视图中删除代码 - 关于关注点的分离以及是否从视图的XAML标记或代码隐藏中挂钩事件处理程序就设计模式而言,完全相同的观点完全没有区别。

编辑但是如果你想保持代码隐藏类的清洁,你可以使用附加行为来实现它:

public class AutoCompleteBoxBehavior
{
    public static ICommand GetSelectionChangedCommand(AutoCompleteTextBox actb)
    {
        return (ICommand)actb.GetValue(SelectionChangedCommandProperty);
    }

    public static void SetSelectionChangedCommand(AutoCompleteTextBox actb, ICommand value)
    {
        actb.SetValue(SelectionChangedCommandProperty, value);
    }

    public static readonly DependencyProperty SelectionChangedCommandProperty =
        DependencyProperty.RegisterAttached(
        "SelectionChangedCommand",
        typeof(ICommand),
        typeof(AutoCompleteBoxBehavior),
        new UIPropertyMetadata(null, OnHandleSelectionChangedEvent));

    private static void OnHandleSelectionChangedEvent(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ICommand command = e.NewValue as ICommand;
        if(command != null)
        {
            AutoCompleteTextBox actb = d as AutoCompleteTextBox;
            actb.Loaded += (ss, ee) =>
            {
                ListBox lb = actb.Template.FindName("PART_ListBox", actb) as ListBox;
                if (lb != null)
                {
                    lb.SelectionChanged += (sss, eee) =>
                    {
                        command.Execute(null);
                    };
                }
            };
        }
    }
}
<ctrls:AutoCompleteTextBox
        Grid.Row="1"
        AutoCompleteQueryResultProvider="{Binding AutoCompleteQueryResultProvider}"
        Margin="10" FontSize="20" PopupHeight="300"
        local:AutoCompleteBoxBehavior.SelectionChangedCommand="{Binding YourCommand}">
 </ctrls:AutoCompleteTextBox>

WPF中的附加行为简介: https://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF