在ListView WPF上编辑TextView列

时间:2016-02-11 16:41:43

标签: c# wpf listview

我正在使用WPF 我有ListView与TextBox列和两个复选框列。
我想通过双击或其他方式编辑TextBox文本 这样做的简单方法是什么?

...
<GridViewColumn Header="Name" DisplayMemberBinding={Binding Path=fullName}" Width=500>
        <GridViewColumn.CellTemplate>
               <DataTemplate>
                      <TextBox Name="txtName"/>
               </DataTemplate>
         </GridViewColumn.CellTemplate>
</GridViewColumn>
...

2 个答案:

答案 0 :(得分:0)

这是一个这样做的示例方式......                                                                                                                                                                                                                                   
                
                          

public partial class MainWindow : Window
{
    public List<string> Items { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        Items = new List<string>();
        LoadItems();
        DataContext = this;
    }

    private void txtName_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        TextBox currentTextBox = (TextBox)sender;
        if (currentTextBox.IsReadOnly)
            currentTextBox.IsReadOnly = false;
        else
            currentTextBox.IsReadOnly = true;
    }

    private void LoadItems()
    {
        Items.Add("Coffee");
        Items.Add("Sugar");
        Items.Add("Cream");
    }
}

<Grid>
    <ListView ItemsSource="{Binding Items}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Name="txtName" Text="{Binding Mode=OneTime}" IsReadOnly="True" MouseDoubleClick="txtName_MouseDoubleClick" Width="100"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>                    
            </GridView>                
        </ListView.View>
    </ListView>
</Grid>

答案 1 :(得分:0)

以下是我编写的应用程序中的示例。 JobName列是用户可编辑的。此示例允许列可编辑,并且还可以删除边框并将背景混合到行中,因此它似乎没有文本框。
这些可以在(BorderThickness="0" Background="Transparent")进行修改。

我的示例绑定到名为JobName的MVVM ViewModel属性,并设置为“TwoWay”,以便对视图模型的更改也将反映在UI上。

 <ListView x:Name="lvJobs" HorizontalAlignment="Left" Height="628" Margin="30,62,0,0" ItemsSource="{Binding Jobs}" 
                  SelectedItem="{Binding SelectedJob, Mode=TwoWay}" VerticalAlignment="Top" Width="335">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Active" Width="50">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding IsActive, Mode=TwoWay}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Job Name" Width="150">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding JobName, Mode=TwoWay}" BorderThickness="0" Background="Transparent"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn DisplayMemberBinding="{Binding User}" Header="User" Width="125"/>
        </GridView>
    </ListView.View>
</ListView>