我有一个自定义DataGridColumn
,可以为editmode手动创建控件,如此
public class DataGridLookupColumn : DataGridTextColumn
{
protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
{
var dataGridBoundColumn = cell.Column as DataGridBoundColumn;
var dockPanel = new DockPanel { HorizontalAlignment = HorizontalAlignment.Stretch };
var textbox = new TextBox { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch };
var button = new Button { HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Center };
DockPanel.SetDock(button, Dock.Right);
dockPanel.Children.Add(button);
dockPanel.Children.Add(textbox);
textbox.Style = cell.FindResource("TextboxWithoutRoundedCorners") as Style;
button.Style = cell.FindResource("LookupButtonRed") as Style;
if (dataGridBoundColumn != null)
{
var cellContent = cell.Content as TextBlock;
var bindingExpression = cellContent != null ? BindingOperations.GetBindingExpression(cellContent, TextBlock.TextProperty) : null;
if (bindingExpression != null)
{
BindLookupCommand(bindingExpression, button);
var newBindning = new Binding(bindingExpression.ParentBinding.Path.Path)
{
UpdateSourceTrigger = bindingExpression.ParentBinding.UpdateSourceTrigger,
Mode = bindingExpression.ParentBinding.Mode
};
textbox.SetBinding(TextBox.TextProperty, newBindning);
}
}
return dockPanel;
}
private void BindLookupCommand(BindingExpression bindingExpression, Button target)
{
ICommand lookupCommand;
if (CommandList.TryGetValue(bindingExpression.ParentBinding.Path.Path, out lookupCommand))
{
var bindning = new CommandBinding(lookupCommand);
target.CommandBindings.Add(bindning);
target.Command = lookupCommand;
}
}
public Dictionary<string, ICommand> CommandList { get; set; }
}
一旦我设置CommandProperty
的{{1}}不能再被点击了。还有什么我需要做的才能让绑定工作吗?
我已经确认我得到了正确的Button
。
由于