【问题标题】:Getting id from selected listview item从选定的列表视图项中获取 id
【发布时间】:2018-11-14 19:59:50
【问题描述】:

我有一个包含此数据模板的 ListView:

<ListView x:Name="lvRitten" Grid.Column="0" Background="Gold" ItemsSource="{Binding ObcRitten}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionChanged="lvRitten_SelectionChanged" SelectionMode="Single">
    <ListView.ItemTemplate>
        <DataTemplate DataType="{x:Type classes:Rit}">
            <Button x:Name="btnride" Height="100" Width="132" >
                <StackPanel>
                    <Grid Height="100" Width="132">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*"/>
                            <ColumnDefinition Width="1*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="1*"/>
                            <RowDefinition Height="3*"/>
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0" Grid.ColumnSpan="2" Background="Aquamarine">
                            <Label Content="naam"/>
                        </Grid>
                        <Grid Grid.Column="0" Grid.Row="1" Background="AntiqueWhite">
                            <StackPanel>
                                <Label Content="{Binding id}" FontSize="10"/>
                                <Label Content="{Binding Naam}" FontSize="10"/>
                                <Label Content="{Binding AantalPassagiers}" FontSize="10"/>
                                <Label Content="{Binding TaxiNummer}" FontSize="10"/>
                            </StackPanel>
                        </Grid>
                        <Grid Grid.Column="1" Grid.Row="1">

                        </Grid>
                    </Grid>
                </StackPanel>
            </Button>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

但现在我想在单击 ListViewItem 时获得位于第三个网格中第一个标签中的 id

获取 id 最简单的方法是什么?

我可以使用绑定将 id 绑定到其他标签吗? 还是我需要做其他事情?

ObcRitten 是一个公共的 ObservableCollection 并且充满了 Rit Rit 示例

Rit rit1 = new Rit
{
    id = 20,
    AantalPassagiers = 5,
    Naam = "Jan",
    TaxiNummer = 1
};

【问题讨论】:

    标签: c# wpf data-binding


    【解决方案1】:

    DataTemplate 中的所有元素共享同一个 DataContext,即Rit item。要在不同位置显示两次id,您需要创建两个标签,并将两者绑定到id

    <Grid Grid.Column="1" Grid.Row="1">
         <Label Content="{Binding id}" FontSize="10"/>
    </Grid>
    

    在 ListView 之外绑定到 ListView.SelectedItem:

    <Label Content="{Binding SelectedItem.id, ElementName=lvRitten}" FontSize="10"/>
    

    【讨论】:

    • 我知道,但我需要列表视图之外的 id,列表视图中还有更多项目
    【解决方案2】:

    在您的 ViewModel 中创建新属性:

    public Rit SelectedRit {get; set;}
    

    然后将此属性与 ListView 绑定:

    <ListView SelectedItem = "{Binding SelectedRit}"..../>
    

    【讨论】:

    • 我不熟悉绑定我应该把属性放在哪里?
    • @Jeffrey 在同一个班级,你的 ObservableCollection 在哪里
    【解决方案3】:

    您可以将SelectedItem 属性转换为Rit 并访问id 属性:

    Rit rit = lvRitten.SelectedItem as Rit;
    if (rit != null)
        var id = rit.id;
    

    您也可以像这样直接将其绑定到TextBlock

    <TextBlock Text="{Binding SelectedItem.id, ElementName=lvRitten}" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多