将页面作为scrollviewer或contentControl等的内容

发布于 2025-02-13 07:47:42 字数 950 浏览 0 评论 0原文

有没有办法将页面放入< grid/>< stackpanel/>< contentControl/>< scrollviewer/>用constructor调用从代码中作为内容?

我期望这样的事情:

xaml:

<Grid>
     <ScrollViewer Content="{Binding Panel0}"/>
</Grid>

c#:

public class TestWindowViewModel : Page
{
   public string Name { get; private set; }
   public string Description { get; private set; }
   public TestWindowViewModel(string name, string description)
   {
       Name = name;
       Description = description;
    }
}

_

public partial class SomeViewModel : Page
{
    public TestWindowViewModel Panel0;

    public SomeViewModel()
    {
        Panel0 = new TestWindowViewModel("panelName", "panelDescription"); 
        InitializeComponent();
    }
}

Is there a way to put a Page inside a <Grid/>, <StackPanel/>, <ContentControl/> or <ScrollViewer/> as content from code using a constructor call?

I expect such things:

XAML:

<Grid>
     <ScrollViewer Content="{Binding Panel0}"/>
</Grid>

C#:

public class TestWindowViewModel : Page
{
   public string Name { get; private set; }
   public string Description { get; private set; }
   public TestWindowViewModel(string name, string description)
   {
       Name = name;
       Description = description;
    }
}

_

public partial class SomeViewModel : Page
{
    public TestWindowViewModel Panel0;

    public SomeViewModel()
    {
        Panel0 = new TestWindowViewModel("panelName", "panelDescription"); 
        InitializeComponent();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若有似无的小暗淡 2025-02-20 07:47:42

您可以使用frame标签,

<ScrollViewer>
<Frame content = "{Binding MyPage}"/>
</ScrollViewer>

如果您不想在ViewModel中有一个道具,那么您应该能够

<ScrollViewer>
<Frame>
<Frame.Content>
<locals:MyPage>
</Frame.Content>
</ScrollViewer>

记住您的东西,即testWindowViewModel继承页面。这不是ViewModel。相反,这是一个普通的页面。

您需要看起来像这样的东西:

    public class NotifyPropertyClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private Page myPage;

        public Page MyPage
        {
            get { return myPage; }
            set
            {
                myPage = value;
                NotifyPropertyChanged();
            }
        }

    }

并且您可以走更远的级别并进行抽象类:

    public abstract class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

然后您可以像这样继承ViewModel:

public class TestWindow: Page
{
   public TestWindow()
   {
       InitializeComponent();
    }
}

public class TestWindowViewModel : ViewModel
    {
        private string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                NotifyPropertyChanged();
            }
        }

        private string description;

        public string Description
        {
            get { return description; }
            set
            {
                Description = value;
                NotifyPropertyChanged();
            }
        }

    }

一旦正确地将其分开,您就可以使用框架并为SomePage和SomePageViewModel,然后您可以在ViewModel的帧内容上使用实际绑定。我知道这很长,但是如果您开始设置一个良好的MVVM设置,那么如果您进入异步,那么您就可以节省自己的头痛,而没有。

You can use a Frame tag

<ScrollViewer>
<Frame content = "{Binding MyPage}"/>
</ScrollViewer>

If you don't want to have a prop in your ViewModel then you should be able to do

<ScrollViewer>
<Frame>
<Frame.Content>
<locals:MyPage>
</Frame.Content>
</ScrollViewer>

Keep in mind you have something called TestWindowViewModel and it inherits Page. This is not a ViewModel. Instead it is a normal page.

You want something that looks like this:

    public class NotifyPropertyClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private Page myPage;

        public Page MyPage
        {
            get { return myPage; }
            set
            {
                myPage = value;
                NotifyPropertyChanged();
            }
        }

    }

and you can go a level farther and make an abstract class:

    public abstract class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

and then you can just inherit ViewModel like so:

public class TestWindow: Page
{
   public TestWindow()
   {
       InitializeComponent();
    }
}

public class TestWindowViewModel : ViewModel
    {
        private string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                NotifyPropertyChanged();
            }
        }

        private string description;

        public string Description
        {
            get { return description; }
            set
            {
                Description = value;
                NotifyPropertyChanged();
            }
        }

    }

Once you get this all seperated out correctly you can use the frame and do the same for the SomePage and SomePageViewModel and then you can use actual binding on the Frame Content from the ViewModel. I know this is long winded, but if you start out right on setting up a good MVVM setup you will save yourself headache if you ever get into Async and what not.

枕梦 2025-02-20 07:47:42

在XAML中:

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
    <Frame x:Name="CurrentPage" NavigationUIVisibility="Hidden"></Frame>
</ScrollViewer>

在CS:

CurrentPage.Content = content;

内容为页面

in xaml:

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
    <Frame x:Name="CurrentPage" NavigationUIVisibility="Hidden"></Frame>
</ScrollViewer>

in cs:

CurrentPage.Content = content;

where content is Page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文