ListBox.SelectedItem选择了错误的行

时间:2016-01-27 05:53:53

标签: c# wpf listbox

我创建ListBox,行可以编​​辑:

 <ListBox Grid.Row="1" x:Name="lbKeys" BorderBrush="Gray" 
                                     ItemsSource="{Binding  Path=Templates}"                                         
                                     IsSynchronizedWithCurrentItem="True" 
                                     Focusable="True"

                                     ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                                     HorizontalContentAlignment="Stretch"
                                     ItemContainerStyle="{StaticResource ResourceKey=lbStyle}">



                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Border BorderThickness="1" BorderBrush="LightGray" Background="WhiteSmoke"
                                                HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                <Grid Name="grEditRow">
                                    <TextBox x:Name="tblbRow" Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"

                                               TextWrapping="Wrap" Margin="2"
                                                       Background="Transparent"
                                                       HorizontalAlignment="Stretch"

                                               />

                                </Grid>
                            </Border>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

但是,当我选择要编辑的行时(我可以这样做) - 我想通过点击按钮来删除这一行:

<Button x:Name="btDelTemplate"  Click="btDelTemplate_Click"  Height="22" Width="22"
                                                    ToolTipService.ShowOnDisabled="True"
                                    ToolTip="{lang:Link LocalePath=RemoveTemplate,DesignValue='Remove row'}"
                                    >
</Button>

它的事件处理程序:

if(lbKeys.SelectedItem!=null)
    RemoveItem(lbKeys.SelectedItem as Row);

但是,所选项目经常出错!据我所知 - 如果我通过点击行的左边框选择项目 - 它运作良好,但是当点击文本框内的行选择项目是错误的。

如何解决? 谢谢!

2 个答案:

答案 0 :(得分:0)

您可以为TextBox的GotFocus编写处理程序,并以编程方式更改SelectedItem。

---------更新----------

private void tblbRow_GotFocus(object sender, RoutedEventArgs e)
    {
        lbKeys.SelectedItem = (sender as TextBox).DataContext as Row;                      
    }

你应该为Row类编写一些代码来让它的对象具有可比性:

public class Row
    {
        public Row() { }
        private string _text;
        public String Text
        {
            get
            {               
                return _text;
            }
        set
        {
            _text = value;                
        }
    }
    public override int GetHashCode()
    {
        return _text;
    }
    public bool Equals(Row r)
    {
        return r._text == _text;
    }
    public override bool Equals(object r)
    {
        Row row = r as Row;
        return row != null && row._text == _text;
    }
}

答案 1 :(得分:0)

我找到了解决方法。 代码如下:

XAML:

<ListBox Grid.Row="1" x:Name="lbKeys" BorderBrush="Gray" 
                                 ItemsSource="{Binding  Templates}"                                         
                                     IsSynchronizedWithCurrentItem="True" 
                                     Focusable="True"

                                     ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                                     HorizontalContentAlignment="Stretch"
                                     ItemContainerStyle="{StaticResource ResourceKey=lbStyle}">



                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border BorderThickness="1" BorderBrush="LightGray" Background="WhiteSmoke"
                                            HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

                                <TextBox x:Name="tblbRow" Text="{Binding Text,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
 Tag="{Binding}"
 GotFocus="tblbRow_GotFocus"                                                       

                                           TextWrapping="Wrap" Margin="2"
                                                   Background="Transparent"
                                                   HorizontalAlignment="Stretch"

                                           />


                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

关键行:

Tag="{Binding}"
GotFocus="tblbRow_GotFocus" 

C#处理程序:

private void tblbRow_GotFocus(object sender, RoutedEventArgs e)
   {
        var textBox = sender as TextBox;
        lbKeys.SelectedItem = textBox.Tag;                       
   }





private void btDelTemplate_Click(object sender, RoutedEventArgs e)
    {
        try
        {

            foreach (var item in lbKeys.Cast<Row>())
            {
                if (item.Template.Id == (lbKeys.SelectedItem as Row).Template.Id)
                {
                    _viewModel.RemoveTemplate(item);
                    break;
                }
            }               
            DataContext = _viewModel;
        }
        catch(Exception ex)
        {
            throw;
        }
    }