Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListBox.SelectedItem选择了错误的行_C#_Wpf_Listbox - Fatal编程技术网

C# ListBox.SelectedItem选择了错误的行

C# ListBox.SelectedItem选择了错误的行,c#,wpf,listbox,C#,Wpf,Listbox,我创建了列表框,其中的行可以编辑: <ListBox Grid.Row="1" x:Name="lbKeys" BorderBrush="Gray" ItemsSource="{Binding Path=Templates}" IsSynchronizedW

我创建了
列表框
,其中的行可以编辑:

 <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>
但是,选择的项目往往是错误的!据我所知,若我通过单击行的左边框来选择项,那个么效果很好,但当单击行内的文本框时,所选项是错误的

如何修复它?
谢谢大家!

您可以为文本框的GotFocus编写处理程序,并以编程方式更改其中的SelectedItem

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

您应该为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;
    }
}
我找到了一个解决办法。 代码如下:

XAML:

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;
        }
    }
private void tblbRow\u GotFocus(对象发送方、路由目标方)
{
var textBox=发送方作为textBox;
lbKeys.SelectedItem=textBox.Tag;
}
私有无效btDelTemplate_单击(对象发送方,路由目标)
{
尝试
{
foreach(lbKeys.Cast()中的var项)
{
if(item.Template.Id==(lbKeys.SelectedItem作为行).Template.Id)
{
_viewModel.RemoveTemplate(项目);
打破
}
}               
DataContext=_viewModel;
}
捕获(例外情况除外)
{
投掷;
}
}

但如何获取所选项目的对象?例如,我将lb.SelectedItem=sender设置为TextBox;(如果可能的话)。但是SelectedItem(行)的类型不是文本,而是行(其中包含文本和其他信息)。因此,如果我有2行2个标识文本-我如何选择项目?写(发件人作为文本框)。DataContext作为YourClass,YourClass将是您设置为项目的绑定项目类型Source我选择我自己的答案,因为它对我有效。谢谢你的回答!
private void tblbRow_GotFocus(object sender, RoutedEventArgs e)
    {
        lbKeys.SelectedItem = (sender as TextBox).DataContext as 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;
    }
}
<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" 
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;
        }
    }