2017-01-17 19 views
0

標題說明了一切。這是我嘗試的,但是當我點擊它們時,新按鈕什麼也不做。我對WPF很感興趣,所以你可以隨意轉發你可以看到的任何明顯的壞習慣。WPF爲每個新按鈕提供使用C#的相同單擊命令

UserInputPage.xaml

<Grid Name="UserInputGrid">    
    <Button Name="FindAddressButton_1" Grid.Row="0" Grid.Column="1" Margin="5,35,0,0" Width="30" Height="20" VerticalAlignment="Top" HorizontalAlignment="left" Click="ClickFindAddress">...</Button> 
    <Button Name="NewFileButton" Grid.Row="1" Margin="10,6,0,0" Width="Auto" Height="18" VerticalAlignment="Top" HorizontalAlignment="Left" Click="ClickNewBar">New Item</Button>   
</Grid> 

UserInputPage.xaml.cs

private void ClickNewBar (object sender, RoutedEventArgs e) 
    { 
     if (FilesList.Items.Count < MAX_ADDRESS_BARS) 
     {     
      CreateNewAddressBar(); 
      CreateFindButton(); 
     } 

     else 
     { 
      MessageBox.Show (ErrorMessages.MAX_ITEMS); 
     } 
    } 

    private void CreateFindButton() 
    { 
     Button newAddressButton = new Button(); 
     newAddressButton.Name = buttonProperties.GetLatestRankedName();   
     newAddressButton.Content = AFindButtonsProperties.CONTENT; 
     newAddressButton.Command = buttonProperties.ButtonsAction; 
     UserInputGrid.Children.Add (newAddressButton); 
    } 

AFindButtonsProperties.cs

public const int ROW = 0; 
    public const int COLUMN = 1; 
    public const double WIDTH = 30; 
    public const double HEIGHT = 20; 
    public const double MARGIN_INCREMENT = 22; 
    public const string CONTENT = "..."; 
    public const string NAME = "FindAddressButton"; 

    public readonly VerticalAlignment verticalAlignment = VerticalAlignment.Top; 
    public readonly HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left; 
    public readonly ICommand ButtonsAction; 

    public AFindButtonsProperties (Grid inputGrid) 
    { 
     gridReference = inputGrid; 
     int notNeeded; 
     buttonReference = GetReferenceLastRankedButton (out notNeeded); 
     ButtonsAction = buttonReference.Command; 
    } 

    private Button GetReferenceLastRankedButton (out int lastRank) 
    { 
     Button outputButton = null; 
     int lastRankConstruction = 0; 

     foreach (var element in gridReference.Children) 
     { 
      Button currentButton = element as Button; 

      if (currentButton != null) //skipps elements that arent buttons 
      { 
       string[] choppedUp = currentButton.Name.ToString().Split ('_'); 

       if (choppedUp.Length > 1 && //prevents exception for names not following the FindButton convention. 
        choppedUp[0].Contains (NAME)) 
       { 
        int numberIsolated = Convert.ToInt16 (choppedUp[1]); 

        if (numberIsolated > lastRankConstruction) 
        { 
         outputButton = currentButton; 
         lastRank = numberIsolated; 
        } 
       } 
      } 
     } 

     if (outputButton == null) 
     { 
      throw new ViewElementNotFoundException ("Cant find any buttons in the grid reference which follow the naming convention."); 
     } 

     else 
     { 
      lastRank = lastRankConstruction; 
      return outputButton; 
     } 
    } 

    public string GetLatestRankedName() 
    { 
     int lastRank; 
     Button lastButton = GetReferenceLastRankedButton (out lastRank); 
     lastRank++; 
     string name = NAME + "_" + lastRank.ToString(); 
     return name; 
    } 
+0

所以這是一個重複的問題,但繼承人我的查詢的答案:http://stackoverflow.com/questions/5102760/wpf-button-click-in-c-sharp-code?rq=1 –

+0

如果你只是想要使用點擊事件爲什麼要打擾命令!我真的迷失在你的問題中! – Emad

+1

由於其摘要工具提示,我認爲Command是點擊事件。你的回答非常好:) –

回答

1

好吧既然你說你想要更好的做法,我把這裏作爲一個答案,但是,知道它沒有解決你的方法。

據我所知,您發佈的代碼片段需要重複多次控制模式(即文本框和按鈕)。要做到這一點,你必須使用適當的控制。這種工作最好的一個是ListView

使用它的最好方法是爲每個元素創建一個模型。例如,讓我們假設你有一個像這樣的模式:

public var list = new ObservableCollection<AModel>(); 

並添加:

public class AModel 
{ 
    public string Text { get; set; } 
    public ICommand ClickCommand { get; set; } 
} 

使用這種模型好吧,我那麼,我想使用它們的頁面創建一個這樣的ObservableCollection使用代碼元素它你已經有了:

private void ClickNewBar (object sender, RoutedEventArgs e) 
{ 
    if (list.Count < MAX_ADDRESS_BARS) 
    {     
     list.Add(new AModel() 
      { 
       Text = "", 
       ICommand = SomeCommand 
      }); 
    } 
    else 
    { 
     MessageBox.Show (ErrorMessages.MAX_ITEMS); 
    } 
} 

,知道你也有一個命令存儲爲SomeCommand。下一步是使用ListView進行渲染。爲此,您需要創建一個項目模板,指定WPF應如何呈現列表中的每個元素。

<ListView ItemsSource={binding list}> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <Grid Name="UserInputGrid">    
       <TextBox Text={binding Text} />   
       <Button Command={binding ClickCommand} /> 
      </Grid> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

你放上這行窗口構造波紋管的InitializeComponents呼叫

this.DataContext = this; 

這是一個正確的做法在WPF動態控制創建你的窗口的數據上下文設置爲自身。更進一步的是使用MVVM並在ViewModels中執行這些工作,這是一種更好的方法。

我希望我能幫你解決你的問題。

+1

我得到了錯誤「在WPF項目中不支持綁定」。將它改爲大寫B綁定工作雖然? :S –