我正在处理WPF应用程序,其中大多数控件是在运行时动态创建的。为了保存一些代码,我创建了各种静态控件。现在,我想创建一个带有删除按钮的文本框控件。我现在卡住的地方是,我如何捕获控件的事件? (看代码)捕获点击类中的通用按钮的事件
编辑: 建议我试着创建一个自定义控件。但是,我无法获得自定义事件的工作。即在wpf页面中,我无法找到删除事件以将处理程序挂接到它。我哪里做错了?到目前为止,我发现,当我放弃: Grid
声明时,会显示事件。
public class tbTextReadOnlyWithDeleteButton : Grid
{
public event EventHandler Delete;
public tbTextReadOnlyWithDeleteButton(int gridrow, int gridcol, string feldname, object quelle, string ctlname, object tag)
{
Grid gr = new Grid() { Name = "grMit_" + gridrow + "_" + gridcol + "_" + ctlname };
gr.SetValue(Grid.RowProperty, gridrow);
gr.SetValue(Grid.ColumnProperty, gridcol);
gr.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(30) });
gr.ColumnDefinitions.Add(new ColumnDefinition());
gr.Tag = tag;
Button bu = new Button() { Name = "buLoeschen" };
bu.SetValue(Grid.ColumnProperty, 0);
bu.Margin = new Thickness(5, 5, 5, 5);
bu.Content = "\xE10A";
bu.FontFamily = new FontFamily("Segoe MDL2 Assets");
bu.FontSize = 10;
bu.Tag = quelle;
bu.Click += Bu_Click;
TextBox tb = new TextBox();
tb.SetValue(Grid.RowProperty, 0);
tb.SetValue(Grid.ColumnProperty, 1);
tb.IsReadOnly = true;
tb.VerticalContentAlignment = VerticalAlignment.Center;
tb.HorizontalContentAlignment = HorizontalAlignment.Left;
tb.Background = new SolidColorBrush() { Opacity = 1 };
tb.BorderThickness = new Thickness(0);
tb.TextWrapping = TextWrapping.Wrap;
BindingOperations.SetBinding(tb, TextBox.TextProperty, new Binding(feldname) { Source = quelle, Mode = BindingMode.OneWay });
gr.Children.Add(bu);
gr.Children.Add(tb);
}
private void Bu_Click(object sender, RoutedEventArgs e)
{
Delete(sender, e);
}
}
// in the page it looks like this:
Grid tbMit = new tbTextReadOnlyWithDeleteButton(1, 0, "Name", mit, "name", dat);
tbMit.Delete // <-- Why can't this be found??
末编辑
// Control
public static Grid tbTextReadOnlyWithDeleteButton(int gridrow, int gridcol, string feldname, object quelle)
{
Grid gr = new Grid();
gr.SetValue(Grid.RowProperty, gridrow);
gr.SetValue(Grid.ColumnProperty, gridcol);
gr.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(30)});
gr.ColumnDefinitions.Add(new ColumnDefinition());
Button bu = new Button() { Name = "buDelete" };
bu.SetValue(Grid.ColumnProperty, 0);
bu.Margin = new Thickness(5, 5, 5, 5);
bu.Content = "\xE10A";
bu.FontFamily = new FontFamily("Segoe MDL2 Assets");
bu.FontSize = 10;
bu.Tag = quelle;
TextBox tb = new TextBox();
tb.SetValue(Grid.RowProperty, 0);
tb.SetValue(Grid.ColumnProperty, 1);
tb.IsReadOnly = true;
tb.VerticalContentAlignment = VerticalAlignment.Center;
tb.HorizontalContentAlignment = HorizontalAlignment.Left;
tb.Background = new SolidColorBrush() { Opacity = 1 };
tb.BorderThickness = new Thickness(0);
tb.TextWrapping = TextWrapping.Wrap;
BindingOperations.SetBinding(tb, TextBox.TextProperty, new Binding(feldname) { Source = quelle, Mode = BindingMode.OneWay });
gr.Children.Add(bu);
gr.Children.Add(tb);
return gr;
}
// Usage in Code
Grid tbMit = glCtlTextbox.tbTextReadOnlyWithDeleteButton(1, 0, "Name", mit);
spStackpanel.Children.Add(tbMit);
//Now I need something like this, which is not working
tbMit.buDelete.Click += buDelete__Click;
有没有人有一丝如何处理这个,?
谢谢!
如果我是你,我会创建一个自定义用户控件 – KANAX