protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { if (string.IsNullOrEmpty(DisplayMemberPath)) { return; } if (element is not Control control) { return; } _ = control.ApplyTemplate(); if (element.ChildOfType <TextBox>() is not TextBox textBox) { return; } BindingOperations.SetBinding(textBox, TextBox.TextProperty, CreateBinding(item)); textBox.MouseLeftButtonDown += TextBox_MouseLeftButtonDown; textBox.GotFocus += TextBox_GotFocus; base.PrepareContainerForItemOverride(element, item); Binding CreateBinding(object item) { return(new Binding { Source = item, Path = new PropertyPath(DisplayMemberPath), Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }); } }
protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { if (element is not Control control) { return; } _ = control.ApplyTemplate(); if (element.ChildOfType <Button>() is not Button button) { return; } if (string.IsNullOrEmpty(CommandPath) == false) { BindingOperations.SetBinding(button, Button.CommandProperty, CreateCommandBinding()); } if (button.Content is not TextBlock textBlock) { return; } if (string.IsNullOrEmpty(DisplayMemberPath) == false) { BindingOperations.SetBinding(textBlock, TextBlock.TextProperty, CreateBinding()); } base.PrepareContainerForItemOverride(element, item); Binding CreateBinding() { return(new Binding { Source = item, Path = new PropertyPath(DisplayMemberPath), Mode = BindingMode.OneWay, }); } Binding CreateCommandBinding() { return(new Binding { Source = item, Path = new PropertyPath(CommandPath), Mode = BindingMode.OneWay, }); } }