WPF XAML ListBox按钮单击不会更改ListView选定的索引

时间:2014-12-02 15:54:11

标签: c# wpf xaml button listbox

我有一个ListBox,在listbox中我有按钮作为项目。 当我点击一个按钮时,我选择的ListBox索引不会改变。

这是我的代码。请指教。

        <ListBox HorizontalAlignment="Left" Height="452" Margin="5.331,12.332,0,0" VerticalAlignment="Top" Width="128.667" SelectionChanged="ListBox_SelectionChanged">
        <Button Content="Theme1" >

            <Button.Background>
             <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
               <GradientStop Color="#FF000000" Offset="0"/>
               <GradientStop Color="#15FFFFFF" Offset="1"/>
             </LinearGradientBrush>
           </Button.Background>
        </Button>
        <Button Content="Theme2" >
                        <Button.Background>
             <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
               <GradientStop Color="#FF000000" Offset="0"/>
               <GradientStop Color="#15FFFFFF" Offset="1"/>
             </LinearGradientBrush>
           </Button.Background>
           </Button>
        <Button Content="Theme3" >
                        <Button.Background>
             <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
               <GradientStop Color="#FF000000" Offset="0"/>
               <GradientStop Color="#15FFFFFF" Offset="1"/>
             </LinearGradientBrush>
           </Button.Background>
         </Button> 
    </ListBox>

1 个答案:

答案 0 :(得分:1)

不完全确定你要完成什么......但这将完成我认为你想要做的事情。这不是我会怎么做的,但如果它是非常简单的事情,它会做你所问的:

<ListBox SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding Buttons}" SelectionChanged="ListBox_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding .}" Click="Button_Click">
                    <Button.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF000000" Offset="0"/>
                            <GradientStop Color="#15FFFFFF" Offset="1"/>
                        </LinearGradientBrush>
                    </Button.Background>
                </Button>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        Buttons = new List<string> { "Theme 1", "Theme 2", "Theme 3" };
        this.DataContext = this;
    }

    private string _selectedItem;
    public string SelectedItem
    { 
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            FirePropertyChanged("SelectedItem");
        }
    }

    private List<string> _buttons;
    public List<string> Buttons
    {
        get { return _buttons; }
        set
        {
            _buttons = value;
            FirePropertyChanged("Buttons");
        }
    }

    #region INotifyPropertyChange

    public event PropertyChangedEventHandler PropertyChanged;

    protected void FirePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

    }

    #endregion

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Button b = sender as Button;
        if (b != null)
        {
            SelectedItem = b.Content as string;
        }
    }

    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }
}