从第一个用户控件的磁贴单击显示第二个用户控件 [英] Showing Second User control from first user control's tile click

查看:60
本文介绍了从第一个用户控件的磁贴单击显示第二个用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have created a Wpf application with mvvm architecture. In main window I have shown a first user control with tile in it. Now I need when I click on a tile of the first user control then it will hide and open second user control in the same Main window and if i close second usercontrol it has to go back to first user control. (Similar to Windows 8 when we click on tile it will open and if we close it will go back to main window)

Can any one tell me ho to achieve this.


i have tried in this way:

on click of tile event i am trying to display second user control.

 private void Tile_Click(object sender, EventArgs e)
        {           
            SecondUserControl.Visibility = Visibility.Visible;      

        }  

but not working :(

推荐答案

在MVVM中如果你想要隐藏和显示你需要设置2个属性的用户控件。



In MVVM if you would like to hide and show user controls you need to set up 2 properties.

public bool IsFirstVisible {get; set;}
public bool IsSecondVisible {get; set;}

public void OnTileClick()
{
    if (IsFirstVisble)
    {
        IsSecondVisible = true;
        IsFirstVisible = false;
    }
    else
    {
        IsSecondVisible = false;
        IsFirstVisible = true;
    }
}





现在,在您的磁贴上,您将切换用户控件的可见性被展示。接下来,您需要使用BoolToVisibility Converter在xaml上反映这一点。





Now, on your tile click, you will toggle the visibility on which user control is being shown. Next you need to reflect this on the xaml with a BoolToVisibility Converter like so.

<window.resources>
    <booleantovisibilityconverter x:key="boolToVis"  />
</window.resources>

<local:firstusercontrol visibility="{Binding IsFirstVisible, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter = {StaticResource boolToVis}}"  />

<local:secondusercontrol visibility="{Binding IsSecondVisible, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter = {StaticResource boolToVis}}"  />;





这应该反映属性为真时的可见状态,以及当pr属性时的崩溃状态operty是假的。



This should reflect a visible state when the property is true, and a collapsed state when the property is false.


这篇关于从第一个用户控件的磁贴单击显示第二个用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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