2016-07-25 5 views
0

대부분의 컨트롤이 런타임에 동적으로 생성되는 WPF 앱에서 작업하고 있습니다. 일부 코드를 저장하기 위해 다양한 정적 컨트롤을 만들었습니다. 이제 텍스트 단추 옆에 삭제 단추가있는 텍스트 상자 컨트롤을 만들려고합니다. 내가 지금 붙어있는 지점은 어떻게 컨트롤의 이벤트를 캡처합니까? (코드 참조)derrived 클래스의 일반 버튼 이벤트 캡처 클릭

편집 : 제안 된대로 사용자 지정 컨트롤을 만들려고했습니다. 그러나 사용자 지정 이벤트가 작동하지 않습니다. 나는. wpf-page에서 delete 이벤트를 찾아 낼 수 없습니다. 나는 어디로 잘못 갔는가? 지금까지 나는 : 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; 

사람이 접근하는 방법의 힌트를 했습니까?

감사합니다.

+0

, 나는 잘 작동 – KANAX

답변

1

당신은 사용자 제어를 정의하는이 개 솔루션이 :

  • 코드 첫 번째 예에서
  • 뒤에

    • XAML + 코드를, 당신은 당신이 캐스팅 삭제 원인을 찾을 수 없습니다 당신의 클래스에서 그리드로.

      그리드 정의에 삭제 이벤트가 없습니다.

      이 작업을 수행해야합니다

      tbTextReadOnlyWithDeleteButton tbMit = new tbTextReadOnlyWithDeleteButton(1, 0, "Name", mit, "name", dat); 
      
      tbMit.Delete += your_event_handler; 
      

      난 당신이 접근 뒤에 XAML + 코드를 사용하는 것이 좋습니다 것입니다. (컨트롤을 정의하기 위해 코드를 사용하는 사람은 아무도 없습니다.)

      Visual Studio에서 프로젝트의 모든 폴더를 클릭하고 사용자 정의 컨트롤을 선택할 수 있습니다.

      자동으로이 파일을해야합니다 :

      • your_control.xaml
      • your_control.xaml.CS

      C + XAML 번호 :

      C 번호 :

      public partial class your_control : UserControl 
      { 
          public delegate void delete_event_handler(your_control sender); 
          public event delete_event_handler delete; 
      
          public your_control() 
          { 
           this.InitializeComponent(); 
          } 
      
          private void on_bu_click(object sender, RoutedEventArgs e) 
          { 
           // the event is null if there is no listeners bind to it 
           if (this.delete != null) 
            this.delete(this); 
          } 
      } 
      

      및 XAML : 다음

      <UserControl x:Class="test_wpf.your_control" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="300"> 
      <Grid> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="3*" /> 
           <ColumnDefinition Width="*" /> 
          </Grid.ColumnDefinitions> 
          <!-- Use the FieldModifier property to define the visibility of the control outside the class--> 
          <TextBlock x:Name="tb" x:FieldModifier="private" >Your text</TextBlock> 
          <Button Grid.Column="1" x:Name="bu" x:FieldModifier="private" Click="on_bu_click">Delete</Button> 
      </Grid> 
      

      ,617,
      public partial class MainWindow : Window 
      { 
          public MainWindow() 
          { 
           InitializeComponent(); 
           your_control control = new your_control(); 
           control.delete += on_delete; 
          } 
      
          public void on_delete(your_control sender) 
          { 
           // your stuff 
          } 
      } 
      

      코드에서만 접근 방법 : 다음

      public class your_control : UserControl 
      { 
      
          public TextBox tb { get; private set; } 
          public Button bu { get; private set; } 
          private Grid container; 
      
          public your_control (/* your params*/) 
          { 
           this.tb = this.build_textbox(); 
           this.bu = this.build_button(); 
           this.container = new Grid(); 
           this.Content = this.container; 
           this.container.Children.Add(this.tb); 
           this.container.Children.Add(this.bu); 
          } 
      
          private TextBox build_textbox() 
          { 
           TextBox tb = new TextBox(); 
           return tb; 
          } 
      
          private Button build_button() 
          { 
           Button bu = new Button(); 
           return bu; 
          } 
      } 
      

      과 : 내가 당신이라면

      public partial class MainWindow : Window 
      { 
          public MainWindow() 
          { 
           InitializeComponent(); 
           your_control control = new your_control(); 
           control.bu.Click += on_bu_click; 
          } 
      
          public void on_bu_click(object sender, EventArgs e) 
          { 
           // your stuff 
          } 
      } 
      
    +0

    사용자 지정 UserControl을을 만들 것입니다. 답답한 답변을 주셔서 대단히 감사합니다. Acpally, 나는 wpf에 대해 매우 익숙하기 때문에 xaml과 cs의 일부에 배치하는 대신 한 곳에서 모든 설정을 갖는 것이 더 편리하다는 것을 알았습니다. :) –