简体   繁体   English

WPF的ListView与文本框错误

[英]wpf Listview with textbox error

I got a listview with a gridview cell template, as a textbox. 我有一个带有gridview单元格模板的listview作为文本框。

I can change the itemssource in the ui at runtime, but I can't do it, in code behind.. 我可以在运行时在ui中更改itemssource,但无法在后面的代码中进行。

Please any ideas why would be a appreciated. 请任何想法为什么会被赞赏。

it's bound to a custom list. 它绑定到一个自定义列表。

this is my code so far: 到目前为止,这是我的代码:

Xaml template. XAML模板。

<Style TargetType="{x:Type TextBlock}" x:Key="GridBlockStyle">
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Visibility" Value="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Converter={StaticResource boolToVis}, ConverterParameter=False}" />
</Style>
<Style TargetType="{x:Type FrameworkElement}" x:Key="GridEditStyle">
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Visibility" Value="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Converter={StaticResource boolToVis}, ConverterParameter=True}" />
</Style>
<DataTemplate x:Key="txt_Field" DataType="{x:Type GridViewColumn}">
            <Grid>
                <TextBlock Text="{Binding Path=txt}" Style="{StaticResource GridBlockStyle}"/>
                <TextBox Style="{StaticResource GridEditStyle}" Text="{Binding Path=txt}"/>
            </Grid>
</DataTemplate>

Code behind: 后面的代码:

DataTemplate t_txt = (DataTemplate)window.FindResource("txt_Field");
gridView.Columns.Add(new GridViewColumn { Header = "txt", CellTemplate = t_txt });

it updates the itemssource and as soon as the foreach loops are done the Entries are being overriden to null. 它会更新itemssource,并且一旦foreach循环完成,条目就会被覆盖为null。

try
{
      foreach (Items1 item in TempList)
      {
           foreach (Items1 item2 in list)
           {
                if (item.num == item2.num)
                {
                      item2.txt = item.txt;
                }
           }
     }
     listview.Items.Refresh();

}
catch { }

I found a solution, but I still want to know why it happent.. 我找到了解决方案,但我仍然想知道为什么会发生..

this is the solution: 这是解决方案:

for (int i = 0; i < TempList.Count; i++)
{
    if (Order_TempList[i].txt == ((Items1)listview.SelectedItem).txt)
    {
        ((Items1)listview.SelectedItem).txt = Order_TempList[i].txt;
        i = TempList.Count;
    }
}

I can see the error very clearly now. 我现在可以很清楚地看到错误了。 It's because there is two lists and the visual list (listview.Items) is being updated and not the physical list (TempList). 这是因为有两个lists并且正在更新visual list (listview.Items),而不是physical list (TempList)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM