2016-02-18 14 views
0

按鈕是以編程方式包含每個文本字符串。也許,有一些方法可以找到像這種情況下的屬性的控件。如何指定一個特定的內容,如「飛機」的WPF按鈕?

這是我的代碼,它以編程方式製作按鈕和鏈接事件的代碼。

如果用戶按下A按鈕(飛機),按鈕的顏色會變成黃色。但是,如果用戶在按下A按鈕後按下B按鈕(Car),該怎麼辦?我想知道如何將A按鈕的顏色從黃色更改爲正常,並將B按鈕的顏色從正常更改爲黃色。這就是爲什麼我需要指出一個按鈕的內容。

for (int k = 0; k < Overviews.Length; k++) 
{ 
    Button btnoverviewcontent = new Button(); 

    ToolTip tt = new ToolTip(); 
    tt.Content = "Press button if you want to modify or delete"; 
    btnoverviewcontent.ToolTip = tt; 

    btnoverviewcontent.Cursor = Cursors.Hand; 

    SolidColorBrush mySolidColorBrush = new SolidColorBrush(); 
    mySolidColorBrush.Color = Color.FromArgb(255, 101, 173, 241); 
    btnoverviewcontent.Background = mySolidColorBrush; 

    btnoverviewcontent.Effect = new DropShadowEffect 
    { 
     Color = new Color { A = 255, R = 0, G = 0, B = 0 }, 
     Direction = 315, 
     ShadowDepth = 5, 
     Opacity = 1 
    }; 

    btnoverviewcontent.Padding = new Thickness(3, 3, 3, 3); 
    btnoverviewcontent.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch; 
    TextBlock textBlock = new TextBlock() 
    { 
     Text = Overviews[k], 
     TextAlignment = TextAlignment.Left, 
     TextWrapping = TextWrapping.Wrap, 

    }; 

    btnoverviewcontent.Content = textBlock; 
    btnoverviewcontent.BorderThickness = new Thickness(0, 0, 0, 0); 
    btnoverviewcontent.FontStretch = FontStretches.UltraExpanded; 
    btnoverviewcontent.Margin = new Thickness(5, 5, 5, 5); 

    WrapPanelGreen.Children.Add(btnoverviewcontent); 
    btnoverviewcontent.Click += new RoutedEventHandler(OnOverviewClick); 
} 
void OnOverviewClick(object sender, RoutedEventArgs args) 
{ 

    buttonOverviewmodification.Visibility = Visibility.Visible; 
    buttonOverviewdeletion.Visibility = Visibility.Visible; 

    Button btn = (Button)sender; 
    DoubleAnimation animation = new DoubleAnimation(1, TimeSpan.FromMilliseconds(350)); 
    buttonOverviewmodification.BeginAnimation(Image.OpacityProperty, animation); 
    buttonOverviewdeletion.BeginAnimation(Image.OpacityProperty, animation); 

    SolidColorBrush mySolidColorBrush = new SolidColorBrush(); 
    mySolidColorBrush.Color = Color.FromArgb(255, 253, 253, 10); 
    (sender as Button).Background = mySolidColorBrush; 
} 
+0

的[?我如何才能找到通過WPF控件名稱或類型(可能的複製http://stackoverflow.com/questions/636383/how-can-i- find-wpf-controls-by-name-or-type) – Gabe

+0

請問您可以擴展您的問題,使其更清楚您要問什麼?示例代碼也非常有用。謝謝! – tpartee

+0

@加貝,對於像我這樣的初學者來說,這太複雜了,也許不會因爲通過名稱或類型找到控件而重複。我知道一種繞開的方式,即編號按鈕,並在某處保存點擊按鈕的數量,並利用編號進行指定。但是,我不知道找到直接和簡單的解決方案。如果這是重複的,請你引導一些例子嗎? –

回答

2

沒有你的代碼,答案是不可行的。根據您的具體情況,這是一個可行的解決方案。

做這樣的事情:

private void OnOverviewClick(object sender, RoutedEventArgs e) 
{ 
    // for all buttons in WrapPanelGreen, reset background to default value we used 
    foreach (var child in WrapPanelGreen.Children) 
    { 
     var b = child as Button; 
     if (b != null) 
     { 
      SolidColorBrush mySolidColorBrush = new SolidColorBrush(); 
      mySolidColorBrush.Color = Color.FromArgb(255, 101, 173, 241); 

      b.Background = mySolidColorBrush; 
     } 
    } 

    // the rest of your code 
} 
+0

哦,這可以是另一種選擇,我非常感謝您的關心和卓越。我從你身上學到了另一個好點子! –

+0

最後,我採用了您的解決方案,並且工作出色。非常滿意。因爲如果我想到上面提到的帖子,找到具有某種條件的控件似乎很複雜。因此,我認爲這不是那麼容易,但它看起來很容易的問題。我把你的解決方案作爲選擇,但足夠明智! –