获取ListView SelectedIndex值中的ListView

时间:2017-05-27 11:07:15

标签: c# wpf listview

它看起来比它实际上要复杂一点,但我不得不在Listview里面使用ListView窗口。我引入了一个DataTemplate作为资源,但问题是我无法访问资源部分中的任何内容,foreach并且去检查但我认为我不知道从哪里开始,我'我必须获取ListView的selectedIndex(可能使用messagebox,比如说你点击了xxx索引)。我使用了 this 示例,我该怎么做?

更新

我的XAML窗口资源

    <Window.Resources>
    <DataTemplate x:Key="InsideListTemplate">
        <StackPanel Orientation="Vertical">
            <Grid MinWidth="300" MinHeight="20">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="100*"/>
                </Grid.ColumnDefinitions>
                <Border Background="{x:Static SystemParameters.WindowGlassBrush}" Grid.ColumnSpan="5" Opacity=".8"/>

                <!--0-->
                <Grid Grid.Row="0" Grid.Column="0" MinWidth="50" MaxWidth="70" HorizontalAlignment="Left" Margin="3,0,0,0">
                    <TextBlock Margin="5,0,0,0" Text="{Binding CodeNumber}" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center"/>
                </Grid>

                <!--1-->
                <Grid Grid.Row="0" Grid.Column="3" MinWidth="300" MaxHeight="50" MinHeight="20" HorizontalAlignment="Left" Margin="5,0,0,0">
                    <Grid>
                        <TextBox Text="{Binding Code}"/>
                    </Grid>
                </Grid>
            </Grid>
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="CodeListTemplate">
        <StackPanel Orientation="Horizontal">
            <Grid Background="{x:Static SystemParameters.WindowGlassBrush}" MinHeight="296" MinWidth="400" MaxHeight="296" MaxWidth="400">
                <StackPanel Orientation="Vertical" VerticalAlignment="Center">
                    <TextBlock Text="{Binding CodeGroupName}" Foreground="White" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </StackPanel>
            </Grid>
            <ListView HorizontalAlignment="Stretch" Background="#FFBB9B45" HorizontalContentAlignment="Stretch" MinWidth="100" MinHeight="25" MaxHeight="300" ItemsSource="{Binding CodeList}" ItemTemplate="{StaticResource InsideListTemplate}"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

主要列表视图

 <ListView x:Name="KodListeleri" Visibility="Visible" Grid.Row="1" Background="CadetBlue" Margin="20,20,20,20" Foreground="#FFFBF7F7" BorderBrush="Transparent" ItemTemplate="{StaticResource CodeListTemplate}" VirtualizingStackPanel.IsVirtualizing="False"/>

public sealed class CodeLibrary
{
    public int CodeGroupNumber { get; set; }
    public string CodeGroupName { get; set; }
    public List<Codes> CodeList { get; set; }
}

public class Codes
{
    public int CodeNumber { get; set; }
    public string Code { get; set; }
}

最后添加一些值

   List<CodeLibrary> AllCodesList = new List<CodeLibrary>();

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        AllCodesList.Add(new CodeLibrary()
        {
            CodeGroupNumber = 1,
            CodeGroupName = "My Code Group 1",
            CodeList = new List<Codes>
            {
                new Codes {CodeNumber = 1, Code = "Code 1"},
                new Codes {CodeNumber = 2, Code = "Code 2"},
                new Codes {CodeNumber = 3, Code = "Code 3"},
                new Codes {CodeNumber = 4, Code = "Code 4"},
                new Codes {CodeNumber = 5, Code = "Code 5"},
                new Codes {CodeNumber = 6, Code = "Code 6"},
                new Codes {CodeNumber = 7, Code = "Code 7"},
                new Codes {CodeNumber = 8, Code = "Code 8"},
                new Codes {CodeNumber = 9, Code = "Code 9"},
                new Codes {CodeNumber = 10, Code = "Code 10"},
                new Codes {CodeNumber = 11, Code = "Code 11"},
                new Codes {CodeNumber = 12, Code = "Code 12"},
                new Codes {CodeNumber = 13, Code = "Code 13"},
                new Codes {CodeNumber = 14, Code = "Code 14"},
                new Codes {CodeNumber = 15, Code = "Code 15"},
                new Codes {CodeNumber = 16, Code = "Code 16"},
                new Codes {CodeNumber = 17, Code = "Code 17"},
                new Codes {CodeNumber = 18, Code = "Code 18"},
                new Codes {CodeNumber = 19, Code = "Code 19"},
                new Codes {CodeNumber = 20, Code = "Code 20"},
            }
        }
        );

        KodListeleri.ItemsSource = AllCodesList;
    }

必须如下所示:enter image description here

1 个答案:

答案 0 :(得分:1)

最快的方法是给ListView一个SelectionChanged事件处理程序:

<ListView 
    SelectionChanged="CodeListView_SelectionChanged"
    ItemsSource="{Binding CodeList}" 
    ItemTemplate="{StaticResource InsideListTemplate}"
    ...

以下是处理程序在窗口类中的样子:

private void CodeListView_SelectionChanged(object sender, 
    SelectionChangedEventArgs e)
{
    var listView = sender as ListView;

    var selectedCodes = listView.SelectedItem as Codes;
    if (selectedCodes != null)
    {
        //  Do stuff with selectedCodes
    }
}

但你可以改为使用MVVM样式:

public sealed class CodeLibrary
{
    public int CodeGroupNumber { get; set; }
    public string CodeGroupName { get; set; }
    public List<Codes> CodeList { get; set; }

    private Codes _selectedCodes;
    public Codes SelectedCodes { 
        get { return _selectedCodes; }
        set {
            if (value != _selectedCodes) {
                _selectedCodes = value;

                //  Do other stuff here if you want
                MessageBox.Info("You selected " + _selectedCodes.Code);
            }
        }
    }
}

XAML:

<ListView 
    SelectedItem
    ItemsSource="{Binding CodeList}" 
    ItemTemplate="{StaticResource InsideListTemplate}"
    ...