在WPF

时间:2017-05-20 05:15:36

标签: c# wpf mvvm-light

目前我正在研究WPF应用程序。在一个页面中我有两个部分(例如part1 | Part2)。在part1中我有项目列表,当我选择任何项目时,我将获得part2中该项目的详细信息。当我减小窗口页面的大小时,我希望弹出详细信息部分。如何实现这一目标?请帮忙!

1 个答案:

答案 0 :(得分:0)

简单,纯粹的WPF实现可以这样工作:

查看:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid x:Name="PartOne" Grid.Column="0" Background="Red"/>
        <Grid x:Name="PartTwoContainer" Grid.Column="1">
            <!--PartTwoControl is just UserControl without any special logic in it-->
            <local:PartTwoControl x:Name="PartTwo" />
        </Grid>
    </Grid>
</Window>

还有一些逻辑:

private const int MaxXToPopup = 300;
private Window _popup;

public MainWindow()
{
    InitializeComponent();
}

protected override void OnRenderSizeChanged( SizeChangedInfo sizeInfo )
{
    if ( ShouldDisplayInPopup() )
    {
        DetachPartTwo();
        OpenPopup();
    }
    else if ( ShouldDisplayInGrid() )
    {
        ClosePopup();
        ShowPartTwo();
    }

    base.OnRenderSizeChanged( sizeInfo );
}

private void ClosePopup()
{
    _popup.Content = null;
    _popup.Close();
    _popup = null;
}

private void OpenPopup()
{
    _popup = new Window {Content = PartTwo};
    _popup.Show();
}

private void ShowPartTwo()
{
    PartTwoContainer.Children.Add( PartTwo );
}

private void DetachPartTwo()
{
    PartTwoContainer.Children.Remove( PartTwo );
}

private bool ShouldDisplayInGrid()
{
    return _popup != null && RenderSize.Width > MaxXToPopup;
}

private bool ShouldDisplayInPopup()
{
    return _popup == null && RenderSize.Width < MaxXToPopup;
}