列表框中的按钮。 [英] Buttons in a List Box.

查看:51
本文介绍了列表框中的按钮。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个包含许多样式按钮的列表框。我能够为每个按钮添加一个Click事件,但问题是ListBox不会以一个Selected Item结束。我认为因为按钮可以阻止事件冒泡。
捕获点击事件最简单的方法是什么,但允许列表框最终得到一个选定的项目?当然,我正试图从另一个控件绑定到Selected Item,但这并不像我想的那样好。

I have a list box that contains a number of styled buttons. I'm able to add a Click event to each of the buttons, but the problem is that the ListBox does not end up with a Selected Item. I think because the button stops the event from bubbling any higher. What's the easiest way to capture the click event, yet allow the listbox to end up with a selected item? Of course I'm trying to bind to the Selected Item from another control and that's not working quite as well as I'd like.

谢谢

Warrick

推荐答案

你好,

这可能会很快变得复杂所以我要去给你一个起点,我想你可以从那里得到它。

This can get complex before long so I am going to give you a starting point and I think you could get it worked out from there.

开始一个新项目并添加以下XAML:

Start a new project and add the following XAML:

	<Grid x:Name="LayoutRoot" >
		<ListBox x:Name="lstItems" Margin="8,8,8,120" SelectionChanged="ListBox_SelectionChanged">
			<Button Content="Button" Width="75" Click="Button_Click"/>
			<Button Content="Button" Width="75" Click="Button_Click"/>
			<Button Content="Button" Width="75" Click="Button_Click"/>
			<Button Content="Button" Width="75" Click="Button_Click"/>
		</ListBox>
		<TextBlock Height="24" Margin="9,0,8,80" TextWrapping="Wrap" Text="{Binding SelectedIndex, ElementName=lstItems}" VerticalAlignment="Bottom"/>
	</Grid>

现在在后面的代码中,添加:

Now in the code behind, add this:

/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			this.InitializeComponent();

			// Insert code required on object creation below this point.
		}

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      Button originator = sender as Button;
      int ListBoxElementCount = 0;

      foreach (Button item in this.lstItems.Items )
      {
        if (item.GetHashCode().Equals(originator.GetHashCode()))
        {
          this.lstItems.SelectedIndex = ListBoxElementCount;
          break;
        }
        ListBoxElementCount++;
      }
    }    

    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      MessageBox.Show("It should be item index " + this.lstItems.SelectedIndex.ToString());
    }
	}

您可以在代码中看到,所有按钮元素共享相同的点击事件处理程序,我们只是看看哪个item作为索引触发,并根据需要在列表框中设置selectedIndex值。

You can see that in the code, all the button elements share the same click event handler and we are simply looking at which item fired as an index and setting the selectedIndex value on the listbox as required.

我还为您在列表框中的selectedIndex上添加了绑定,以便在文本框中显示。

I also put in the binding for you for the selectedIndex on the listbox to show in the textbox.

希望这有帮助

 

干杯


这篇关于列表框中的按钮。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆