列表框.选定项选择了错误的行

本文关键字:错误 选择 列表 | 更新日期: 2023-09-27 18:31:59

我创建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);

但是,选择的项目往往是错误的!据我了解 - 如果我通过单击行的左边框来选择项目 - 它工作得很好,但是当单击行内的文本框时 - 所选项目是错误的。

如何解决?谢谢!

列表框.选定项选择了错误的行

我找到了一个解决方法。这里的代码:

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;
        }
    }

您可以为 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;
    }
}