Listview:如何只编辑其中一个标签字段

时间:2017-01-03 14:45:08

标签: c# wpf listview

我有一个动态创建项目列表的列表视图。在列表视图中,每个成员都有2个标签:“等级”(数字,通常为1-10)和“名称”。每个listview项目也有3个按钮。我想这样做,以便用户能够编辑“排名”标签。如果他们也能够编辑名称标签,那就没关系,但我更喜欢那个。

我在网上看到的答案都涉及ListView的“LabelEdit”属性,但是对我来说这似乎不存在,因为当我包含该属性时我遇到了编译错误。

这是我的ListView的Xaml:

 <ListView Margin="134,110,100,50" 
              Name="carrierList" 
              HorizontalAlignment="Center" 
              Width="232" 
              BorderBrush="LightGray" 
              BorderThickness="1,1,1,1" 
              Visibility="{Binding Path=ShowCarrierList, Converter={StaticResource BoolToVis}}" 
              ItemsSource="{Binding CarrierList}" 
              Grid.RowSpan="2" 
              ScrollViewer.CanContentScroll="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                    <DockPanel Margin="0,0,0,0" HorizontalAlignment ="Stretch">
                        <Grid HorizontalAlignment="Stretch">
                            <Grid.Resources>
                                <Style TargetType="{x:Type Border}">
                                    <!-- All rows -->
                                    <Setter Property="BorderBrush" Value="Black" />
                                    <Setter Property="BorderThickness" Value="2" />
                                    <Setter Property="CornerRadius" Value="5" />
                                </Style>
                            </Grid.Resources>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="165" />
                                <ColumnDefinition Width="20" />
                                <ColumnDefinition Width="20" />
                                <ColumnDefinition Width="20" />
                            </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding Rank}" />
                        <TextBlock Text="{Binding CarrierName}" Margin="20,0,0,0"/>


                        <Button  Width="20" Grid.Column="1" HorizontalAlignment="Right" ToolTip="Edit Carrier Details" Command="{Binding ElementName=carrierList, Path=DataContext.EditCarrierClick}" CommandParameter="{Binding}">
                                <StackPanel>
                                    <Image Source="{StaticResource EditImg}"/>
                                </StackPanel>
                            </Button>
                        <Button  Width="20" Grid.Column="2" HorizontalAlignment="Right" ToolTip="View All Nodes With This Carrier" Command="{Binding ElementName=carrierList, Path=DataContext.AffectedNodeListClick}" CommandParameter="{Binding}">
                                <StackPanel>
                                    <Image Source="{StaticResource NetworkImg}"/>
                                </StackPanel>
                            </Button>
                        <Button  Width="20" Grid.Column="3" HorizontalAlignment="Right" ToolTip="Remove Carrier From Node" Command="{Binding ElementName=carrierList, Path=DataContext.OnRemoveClick}" CommandParameter="{Binding}">
                                <StackPanel>
                                    <Image Source="{StaticResource ErrorImg}"/>
                                </StackPanel>
                        </Button>
                        </Grid>
                    </DockPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

1 个答案:

答案 0 :(得分:1)

替换&#34; Rank&#34;使用TextBox标记并确保&#34; Rank&#34; source属性有一个公共setter:

7