在网格视图中引用单元格中的TextBox

时间:2017-04-28 07:55:54

标签: c# wpf listview data-binding

所以我在其视图道具中使用ListView制作了GridView

<ListView Grid.Row="1" Name="TicketListView" Margin="20,5,20,5">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Owned" DisplayMemberBinding="{Binding Owned}"/>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
                <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price, ConverterCulture='en-US', StringFormat={}{0:C2}}"/>
                <GridViewColumn Header="Amount">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox MinWidth="20" TextAlignment="Center" Margin="5" Text="{Binding Bought, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>                
        ...
</ListView>

我想在此列表视图中选择项目时专注于特定TextBox,因此您无需单击TextBox进行编写,而是可以单击项目(行)并开始更改它的价值。 (希望你理解;()

这是可能的,还是应该用另一种方式列出?

2 个答案:

答案 0 :(得分:1)

您可以处理PreviewMouseLeftButtonDown的{​​{1}}事件,并使用ListViewItem类获取对TextBox的引用:

VisualTreeHelper
<ListView Grid.Row="1" Name="TicketListView">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="TicketListView_PreviewMouseLeftButtonDown" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Owned" DisplayMemberBinding="{Binding Owned}"/>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price, ConverterCulture='en-US', StringFormat={}{0:C2}}"/>
            <GridViewColumn Header="Amount">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox MinWidth="20" TextAlignment="Center" Margin="5" Text="{Binding Bought, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

答案 1 :(得分:-2)

在listitem上更改了事件 做,

TextBox.focus();
TextBox.text=List1.selectedValue.Tostting();