XAML 通过将“IsVisible”设置为“False”来隐藏avalonia ui中的网格列

sczxawaw  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(185)

在我的avalonia UI应用程序(MVVM,React式和桌面上),我想拆分我的窗口,并在另一个窗口/屏幕上显示一些视觉效果,而它们应该隐藏在我的主窗口上。第二个窗口的创建工作,但隐藏在我的主窗口上的用户控件的视觉效果不工作。
总的来说,这就是我的代码,这里是我的控件的视图模型,包括我想要显示/隐藏的所有视觉效果:

namespace myavaloniaapp.ViewModels
{
    public class UserControl1ViewModel : ReactiveObject
    {

        string WindowMode { get; set; } 

        public UserControl1ViewModel()
        {
             OnButtonClick9Async = ReactiveCommand.Create(splitwindow);
             //further stuff is initialized here
        }

        private void splitwindow()
        {

            SecondaryWindow win2 = new SecondaryWindow(); //create a second window...
            win2.Show();
            win2.DataContext = this;  //...and set the new window's datacontext also to this viewmodel
            WindowMode = "False";  // this should hide the grid column in which the visuals that should be hidden are                   

        }
    }
}

UserControl 1的.axaml如下所示:

<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:views="using:myavaloniaapp.Usercontrols"
             xmlns:vm="using:myavaloniaapp.ViewModels"
             x:Class="myavaloniaapp.Usercontrols.UserControl1">

    <Grid HorizontalAlignment="Stretch" DataContext="{Binding UserControl1ViewModel}">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Grid Grid.Column="0">
            
                  <Button Command="{Binding OnButtonClick9Async}">Split</Button><!--the button that should trigger the split of the window-->

                  <!--some visuals in the first column that should always remain visible in this window-->
                
            <Grid Grid.Column="1" IsVisible="{Binding WindowMode}">
            
                  <!--here the visuals that should be hidden when pressing the button by hiding the entire grid column-->
        
            </Grid>
    </Grid>
</UserControl>

然而,更改网格列的“IsVisible”属性并没有做任何事情。经过一些研究,我想也许UI需要重新绘制,所以我把这个添加到我的splitwindow方法中:

if (Avalonia.Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) 
        {
            desktop.MainWindow.InvalidateVisual();
        }

但这并没有帮助。有趣的是,当我在视图模型的初始化中将“IsVisble”属性设置为“False”时,更改“IsVisble”属性会起作用,所以我猜问题可能与UI在绘制后没有实现属性的更改有关。希望你能告诉我哪里做错了!

8ljdwjyq

8ljdwjyq1#

根据radoslawik的评论,需要在视图模型中添加RaiseAndSetIfChanged

private string windowmode;

public string WindowMode
{
       get => windowmode;
       set => this.RaiseAndSetIfChanged(ref windowmode, value); 
}

IsVisible可以是stringbool,都可以。

相关问题