【发布时间】: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 个以编程方式制作的按钮,每个按钮的内容是“飞机”、“汽车”、“火箭”。然后,我如何以编程方式知道(指示)带有“飞机”的按钮。
-
找到按钮后,你会用它做什么?我猜这是您想要回答的真正问题,但是如果没有一些代码或您在做什么的想法,就无法提供诸如使用事件、属性、标签等的答案。