【问题标题】:How to indicate a WPF button with specific content like 'airplane'?如何指示具有特定内容(如“飞机”)的 WPF 按钮?
【发布时间】:2016-02-18 02:05:01
【问题描述】:

按钮以编程方式包含每个文本字符串。也许,有一些方法可以找到像这种情况下具有其属性的控件。

这是我以编程方式制作按钮的代码和链接事件的代码。

如果用户按下 A 按钮(飞机),按钮的颜色变成黄色怎么办。但是,如果用户在按下 A 按钮后按下 B 按钮(汽车)怎么办?我想知道如何将 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;
}

【问题讨论】:

  • 能否请您扩展您的问题以更清楚地说明您在问什么?示例代码也将非常有用。谢谢!
  • @Gabe,对于像我这样的初学者来说,这太复杂了,而且可能不会与按名称或类型查找控件重复。我知道一种绕过方式,即编号按钮并将单击按钮的数量保存在某处并利用该数字来指定。但是,我想找到直接和更简单的解决方案。如果这是重复的,请您指导一些例子吗?
  • @tpartee,对不起,没有例子,但这是很容易的情况。有 3 个以编程方式制作的按钮,每个按钮的内容是“飞机”、“汽车”、“火箭”。然后,我如何以编程方式知道(指示)带有“飞机”的按钮。
  • 找到按钮后,你会用它做什么?我猜这是您想要回答的真正问题,但是如果没有一些代码或您在做什么的想法,就无法提供诸如使用事件、属性、标签等的答案。

标签: c# wpf button


【解决方案1】:

没有您的代码,答案是不可行的。根据您所拥有的,这是一个可行的解决方案。

做这样的事情:

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
}

【讨论】:

  • 哦,这可以作为替代方案 我非常感谢您的关心和卓越。多亏了你,我又学到了一个好点!
  • 最后,我采用了您的解决方案,而且效果很好。很满意。因为如果我想到上面提到的帖子,找到具有某种条件的控件似乎很复杂。因此,我认为这不会那么容易,尽管它看起来很简单。我将您的解决方案作为替代方案,但足够明智!
猜你喜欢
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
  • 2015-06-27
  • 2013-03-25
  • 1970-01-01
  • 2021-09-21
  • 2015-04-17
  • 1970-01-01
相关资源
最近更新 更多