亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

向網格 MVVM Caliburn 添加更多用戶控件

向網格 MVVM Caliburn 添加更多用戶控件

C#
GCT1015 2022-12-31 13:20:11
我正在建造一個駕駛艙,我有很多用戶控制(不同類型的開關),我試圖將它們集成到一個窗口中我正在使用 caliburn 和 Ninject 并嘗試保留 MVVM。所以我有一個問題,我必須將不同的開關動態集成到窗口的網格中,我不知道我是否可以保留 MVVM所以在我的解決方案中,我使用網格的名稱將不同的用戶控件放置在不同的位置,并且我打破了 MVVM我怎么能用 MVVM 做到這一點?我讀過我可以使用 ContentControl 從列表中綁定不同的 ViewModel,但我不知道該怎么做。歡迎提供幫助引導程序.cs:using Caliburn.Micro;using Ninject;using System;using System.Collections.Generic;using System.Windows;using System.Windows.Input;using TestNinjectCaliburn.ViewModels;using EventAggregator = TestNinjectCaliburn.Events.EventAggregator;using IEventAggregator = TestNinjectCaliburn.Events.IEventAggregator;namespace TestNinjectCaliburn{    public class Bootstrapper : BootstrapperBase    {        private IKernel kernel;        public Bootstrapper()        {            Initialize();        }        protected override void Configure()        {            kernel = new StandardKernel();            kernel.Bind<IEventAggregator>().To<EventAggregator>().InSingletonScope();            kernel.Bind<IWindowManager>().To<WindowManager>().InSingletonScope();            kernel.Bind<MainWindowViewModel>().ToSelf().InSingletonScope();            MessageBinder.SpecialValues.Add("$pressedkey", (context) =>            {                // NOTE: IMPORTANT - you MUST add the dictionary key as lowercase as CM                // does a ToLower on the param string you add in the action message, in fact ideally                // all your param messages should be lowercase just in case. I don't really like this                // behaviour but that's how it is!                var keyArgs = context.EventArgs as KeyEventArgs;                if (keyArgs != null)                    return keyArgs.Key;                return null;            });        }
查看完整描述

1 回答

?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

網格是這里使用的錯誤面板。如果您正在設計駕駛艙,那么您已經知道元素的位置,因此您不需要 WPF 來為您進行布局。因此,使用 Canvas。您可能希望您的駕駛艙隨視圖縮放,因此根據您自己選擇的任意單位(例如 1000x1000)為其指定一個大小,并將整個內容包裝在一個 Viewbox 中。


關于元素的實際呈現,您在屏幕上呈現控件列表,每當您這樣做時,您的第一直覺應該是使用 ItemsControl。因此,您將從駕駛艙元素的某種類型的基本視圖模型開始,然后將它們全部放在一個列表中。要顯示它們,您可以使用 ItemsControl,將 ItemsSource 綁定到您的列表。您希望在 Canvas 上顯示所有控件,因此模板化 ItemControl 的 ItemsPanel。最后,您需要指定每個元素在屏幕上的位置,因此為您的基本視圖模型類提供 X/Y 屬性并綁定到 ItemControl 的 ItemContainerStyle 中的那些。把所有這些放在一起,你會得到這個:


<Viewbox>

    <ItemsControl ItemsSource="{Binding MyCockpitViewModels}" Width="1000" Height="1000">


        <!-- Replace panel with a canvas -->

        <ItemsControl.ItemsPanel>

            <ItemsPanelTemplate>

                <Canvas />

            </ItemsPanelTemplate>

        </ItemsControl.ItemsPanel>


        <!-- Set position of each element in the canvas -->

        <ItemsControl.ItemContainerStyle>

            <Style TargetType="{x:Type ContentPresenter}">

                <Setter Property="Canvas.Left" Value="{Binding X}" />

                <Setter Property="Canvas.Top" Value="{Binding Y}" />

            </Style>

        </ItemsControl.ItemContainerStyle>


    </ItemsControl>

</Viewbox>

如果你運行它,你會看到視圖模型的名稱顯示在它們各自的畫布位置,所以剩下要做的唯一一件事就是以某種方式告訴 WPF 要為每個元素而不是文本繪制哪些控件。這是通過 DataTemplates 完成的,您可以將其放置在可視樹中的任何位置,例如 app.xaml 的 ResourceDictionary,或者用于 MainWindow 的那個,或者更好的是放在 ItemsControl 的資源塊中:


<DataTemplate DataType="{vm:SwitchOffOn_ViewModel}">

    <controls:SwitchOffOn_View />

</DataTemplate>

自從我使用 Micro 以來已經有一段時間了,它實際上可能會為您執行此 DataTemplating,但如果沒有,請按照我在此處為您的每個控件顯示的那樣明確聲明它,您將擁有一個完全呈現的駕駛艙。


查看完整回答
反對 回復 2022-12-31
  • 1 回答
  • 0 關注
  • 97 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號