在多列ListView中选择值

时间:2012-07-29 04:25:56

标签: c# wpf xaml listview

我有一个带有listview的弹出窗口:

    <Popup Name="PopupResultados" Width="200" PlacementTarget="{Binding ElementName=textBoxBuscar}" AllowsTransparency="True" PopupAnimation="Slide">
        <ListView Name="listViewResultados" Background="LightGray" BorderThickness="2" SelectionChanged="listViewResultados_SelectionChanged">
            <ListView.View>
                <GridView x:Name="gridViewResultados" ColumnHeaderContainerStyle="{StaticResource noHeader}">
                    <GridViewColumn Header="Col1" Width="0" DisplayMemberBinding="{Binding Col1}" />
                    <GridViewColumn Header="Col2" DisplayMemberBinding="{Binding Col2}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Popup>

列表视图中填充了字典:

    private void buscar(object sender, RoutedEventArgs e)
    {
        Dictionary<int, String> dicc = admin.buscarEntidad(textBoxBuscar.Text);

        if (!PopupResultados.IsOpen)
        {
            if (dicc.Count() != 0)
                foreach (var par in dicc)
                    listViewResultados.Items.Add(new { Col1 = par.Key, Col2 = par.Value });
            else
                listViewResultados.Items.Add(new { Col2 = "No hay coincidencias" });
            PopupResultados.IsOpen = true;
        }
    }

现在,我想在列表视图中选择一个项目,并在Col1中获取整数值。

如何获取整数值?

当我输入listViewResultados.SelectedItem.时,我只获得4种方法:EqualsGetHashCodeGetTypeToString

忘了说我正在尝试获取listViewResultados_SelectionChanged中的整数值

1 个答案:

答案 0 :(得分:1)

您好将listViewResultados.SelectedItem转换为您的ListView ItemsSource绑定到的收集项的类型。就像你的ItemsSource被绑定到字符串List一样,就像(string)listViewResultados.SelectedItem一样。我希望这会有所帮助。