Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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# WPF如何使列表框项目可编辑?_C#_Wpf_Xaml_Listbox - Fatal编程技术网

C# WPF如何使列表框项目可编辑?

C# WPF如何使列表框项目可编辑?,c#,wpf,xaml,listbox,C#,Wpf,Xaml,Listbox,我需要将“编辑所选项目”功能添加到我的列表框中。基本上,我需要两种选择之一: 按住Ctrl键并单击列表项,它会神奇地变成一个可编辑的文本框或其他东西,编辑后会更新数据库中的数据 按下我的列表框下的“编辑”按钮,打开新的编辑窗口,编辑那里的数据,更新数据库,并在编辑窗口关闭时更新列表框 我遇到的问题: 我想要解决方案的第一个变体,但我真的不知道如何实现它 实现第二个变量时,我不知道在新窗口中编辑所选项目后如何更新列表框 这是我的列表框的XAML: <Grid> <

我需要将“编辑所选项目”功能添加到我的列表框中。基本上,我需要两种选择之一:

  • 按住Ctrl键并单击列表项,它会神奇地变成一个可编辑的文本框或其他东西,编辑后会更新数据库中的数据

  • 按下我的列表框下的“编辑”按钮,打开新的编辑窗口,编辑那里的数据,更新数据库,并在编辑窗口关闭时更新列表框

我遇到的问题:

  • 我想要解决方案的第一个变体,但我真的不知道如何实现它

  • 实现第二个变量时,我不知道在新窗口中编辑所选项目后如何更新列表框

这是我的列表框的XAML:

<Grid>
    <ListBox x:Name="LstQueries" HorizontalAlignment="Left" Height="228" VerticalAlignment="Top" Width="482" FontFamily="HelveticaNeueCyr" FontSize="16" ItemsSource="{Binding Queries}" MouseDoubleClick="LstQueries_MouseDoubleClick">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Label Content="{Binding Name}" FontWeight="Medium" FontSize="18" FontFamily="Helvetica"/>
                    <TextBlock Text="{Binding Text}" FontSize="16" FontFamily="Helvetica"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <Button x:Name="BtnEdit" Content="Edit" HorizontalAlignment="Left" Margin="0,228,0,0" VerticalAlignment="Top" Width="482" Height="43" Click="BtnEdit_Click"/>
</Grid>

那么,您能帮我实现这两种解决方案吗?

为什么要使用dynamic?您可以毫无问题地将
SelectedItem
转换为
Query
。一旦你有了它,你所需要做的就是将属性加载到你的编辑窗口中,并在这里进行编辑

您的查询需要实现
INotifyPropertyChanged
。否则,您将不会在
列表框中看到更改(因为控件不知道这些更改)

而且,使用
绑定将更容易。不要忘记将绑定模式设置为双向
,以便在更改属性后更新属性


为了你的窗户。准备一个
Query
属性(例如命名为SelectedQuery),将所选实例设置为该属性。然后,您可以使用如下方式绑定文本框:
Text=“{Binding SelectedQuery.Name,Mode=“TwoWay”}”
。如果您正确地实现了
INotifyPropertyChanged
更改窗口中的文本应该会立即更改
列表视图中的文本

,您可以用不同的方法解决这个问题。 在我曾经写过的一个项目中,我想双击一个项目,打开该项目的编辑窗口。 我选择使用
I.Interaction.Triggers
。具体代码如下所示:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseDoubleClick">
        <Command:EventToCommand Command="{Binding EditExercise_Command}" 
                  CommandParameter="{Binding ElementName=LastExercises_ListView
                                            ,Path=SelectedItem}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

如果您不想双击,或者如果您想删除该项目(使用不同的命令),您还可以获得额外的右键单击菜单选项。

因此,您有您选择的项目,您有一个编辑按钮,我不太确定问题出在哪里。。。Filip的回答似乎也很公平。@Noctis好吧,问题是在构造函数或类似的东西中在窗口之间来回传递数据是一个相当丑陋的解决方案,我想知道在这种情况下如何有效地使用绑定。但是第一个解决方案(神奇的解决方案)无论如何都会好得多:)您能详细介绍一下这些绑定吗?我已经阅读了您提供的文章,并在我的
Query
类中实现了
INotifyPropertyChanged
。我还在ListBox.ItemTemplate绑定中添加了
Mode=TwoWay
。但是我应该怎么做才能真正地将数据传递到新窗口并使它们显示在那里,然后,在单击“保存”按钮之后,如何更新列表框?我到底应该在哪里添加这些绑定?是的,现在一切都很好!但是如果我想取消编辑,我应该怎么做?我的意思是,我只想在单击“保存”按钮后进行更改,但在我在“bew”窗口中编辑数据时,这些更改就会发生。@DenisYakovenko您可以将原始值保存在某个位置,并且在未单击“保存”按钮时还原这些值以供示例。
if (LstQueries.SelectedIndex < 0) return;
dynamic item = LstQueries.SelectedItem as dynamic;
string name = item.Name;
string text = item.Text;
public class Query
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Text { get; set; }
    public string Autoschool { get; set; }
}
<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseDoubleClick">
        <Command:EventToCommand Command="{Binding EditExercise_Command}" 
                  CommandParameter="{Binding ElementName=LastExercises_ListView
                                            ,Path=SelectedItem}" />
    </i:EventTrigger>
</i:Interaction.Triggers>
   <ListBox x:Name="..."  
              ItemsSource="{Binding FilteredCollection}"
              SelectedItem="{Binding SelectedExercise, UpdateSourceTrigger=PropertyChanged}"
              ...
              >
        <ListBox.ContextMenu>
            <ContextMenu>
                <MenuItem Header ="Edit Exercise"   
                          Command="{Binding EditExercise_Command}" 
                          CommandParameter="{Binding SelectedExercise}"
                />
                <MenuItem Header ="Delete Exercise" 
                          Command="{Binding DeleteExercise_Command}" 
                          CommandParameter="{Binding SelectedExercise}"
                />
            </ContextMenu>
        </ListBox.ContextMenu>

        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <Command:EventToCommand Command="{Binding EditExercise_Command}" 
                                        CommandParameter="{Binding ElementName=LastExercises_ListView
                                                                 , Path=SelectedItem}" 
                />
            </i:EventTrigger>
        </i:Interaction.Triggers>

    </ListBox>