WPF:最简单的方法的基础上选择的ListView控件添加到网格 [英] WPF: Easiest way to add control to grid based on ListView selection

查看:115
本文介绍了WPF:最简单的方法的基础上选择的ListView控件添加到网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView 它包含了应该重新present某些设置,它可以由用户更改的项目。我寻找到 ListViewItem的链接到一个自定义用户控件的最简单的方法。

I have a ListView which contains items that should represent certain settings, which can be changed by the user. I am looking for the easiest way to link the ListViewItem to a custom user control.

我现在有什么:

    <ListView 
        Grid.Row="0" 
        Grid.Column="0"
        SelectionChanged="SettingsListViewSelectionChanged">

        <ListViewItem x:Name="PathSettings" Content="Path"/>
        <ListViewItem x:Name="HideShowTvShows" Content="Hide/Show TV Shows"/>
    </ListView>

然后在code后面我弄清楚单击了哪个项目,并附加相应的用户控件电网

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

        if (listView != null)
        {
            SettingsContentPanel.Children.Clear();

            if (listView.SelectedItem.Equals(_pathSettings))
            {
                SettingsContentPanel.Children.Add(_pathSettings);

                _pathSettings.SetValue(Grid.RowProperty, 0);
                _pathSettings.SetValue(Grid.ColumnProperty, 1);
            }
            if (listView.SelectedItem.Equals(_hideShowTvShowsSettings))
            {
                SettingsContentPanel.Children.Add(_hideShowTvShowsSettings);

                _hideShowTvShowsSettings.SetValue(Grid.RowProperty, 0);
                _hideShowTvShowsSettings.SetValue(Grid.ColumnProperty, 1);
            }
        }
    }

和网格本身:

    <Grid 
        x:Name="SettingsContentPanel" 
        Grid.Row="0" 
        Grid.Column="2" 
        Grid.ColumnSpan="2" />

有没有摆脱后面的锅炉板code,并使用XAML为这个目的是什么?一种方法

Is there a way to get rid of the boiler plate code behind and use XAML for this purpose?

推荐答案

看起来你正在寻找一个内容presenter

Looks like you're looking for a ContentPresenter:

<ContentPresenter x:Name="SettingsContentPanel"
                  Content="{Binding SelectedItem, ElementName=MyLstView}"/>

这篇关于WPF:最简单的方法的基础上选择的ListView控件添加到网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆