首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AvaloniaUI - VIewModel不能绑定到用户控件

AvaloniaUI - VIewModel不能绑定到用户控件
EN

Stack Overflow用户
提问于 2022-10-25 13:06:37
回答 1查看 56关注 0票数 0

我目前正在使用AvaloniaUI和C#.net开发一个应用程序。我的应用程序有一个MainWindow,它使用一个ViewModel(称为MainWindowViewModel),该窗口还包含两个通过TabControl集成的UserControls。

所以我现在的问题是,我想给每个UserControl自己的ViewModel,到目前为止,我已经为我的一个UserControls创建了一个ViewModel,并且还在我的控件的axaml中将名称空间设置为它。我也设置了DataContext,但从未加载ViewModel。

如下所示,是我的MainWindow、UserControl和my UserControl的ViewModel的源代码:

MainWindow.axaml

代码语言:javascript
运行
复制
<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:MyApp.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:views ="clr-namespace:MyApp.Views"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="MyApp.Views.MainWindow"
    WindowStartupLocation="CenterScreen"
        Icon="/Assets/Programmicon.png"
        Title="{Binding WindowTitle}" CanResize="False" >
    <Design.DataContext>
        <vm:MainWindowViewModel/>
    </Design.DataContext>
        <Window.Styles>
            <Style Selector="TabItem">
                <Setter Property="FontSize" Value="16"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Height" Value="34"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Background" Value="#148198"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="Margin" Value="0 0 0 0"/>
                <Setter Property="Padding" Value="10 0"/>
            </Style>
            <Style Selector="TabControl WrapPanel">
                <Setter Property="Background" Value="#148198"/>
            </Style>
            <Style Selector="TabItem:selected">
                <Setter Property="Background" Value="White"/>
                <Setter Property="Foreground" Value="Black"/>
                <Setter Property="Margin" Value="0 0 0 0"/>
                <Setter Property="Padding" Value="10 0"/>
            </Style>
        </Window.Styles>
        <Grid>
        <TabControl Name="tabMenu" Background="White">
            <TabItem Header="Import" VerticalContentAlignment="Center" >
                <views:ImportView/>
            </TabItem>
            <TabItem Header="Einstellungen" VerticalContentAlignment="Center">
                <views:SettingsView/>
            </TabItem>
        </TabControl>
            <Label Name="lblErrorInfo" Content="" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" ZIndex="10" Background="Red" Foreground="White" FontSize="34" FontWeight="Bold" IsVisible="false"></Label>
    </Grid>
</Window>

SettingsView.axaml

代码语言:javascript
运行
复制
<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:vm="using:MyApp.ViewModels"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="MyApp.Views.SettingsView">
    <this.DataContext>
        <vm:SettingsViewModel/>
    </this.DataContext>
        <DataGrid Name="gEventlog" Items="{Binding EventlogData}" AutoGenerateColumns="False" CanUserResizeColumns="False" CanUserReorderColumns="False" Background="LightGray" CanUserSortColumns="True" Canvas.Top="20" Width="1024" Height="626" GridLinesVisibility="All" IsReadOnly="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" AlternatingRowBackground="Azure">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="ID"/>
                    <DataGridTextColumn Header="Zeitstempel"/>
                    <DataGridTextColumn Header="Event-Typ"/>
                    <DataGridTextColumn Header="Benutzer"/>
                    <DataGridTextColumn Width="563" Header="Fehlermeldung"/>
                    <DataGridTextColumn Header="Funktion"/>
                </DataGrid.Columns>
        </DataGrid>
</UserControl>

SettingsViewModel.cs

代码语言:javascript
运行
复制
using MyApp.Classes;
using Microsoft.Data.Sqlite;
using System;
using System.Data;

namespace MyApp.ViewModels
{
    public class SettingsViewModel : ViewModelBase
    {
        // The appconfig class
        private readonly AppConfiguration _appConfig;
        // The utils class
        private readonly Utils _utils;
        // The eventlog class
        private readonly Eventlog _event;

        private string _importFilesPath;

        // The data of the eventlog-grid
        private DataView _eventlogData;

        public DataView EventlogData
        {
            get { return _eventlogData; }
            set
            {
                if (_eventlogData == value)
                {
                    return;
                }

                _eventlogData = value;
            }
        }

        public string ImportFilesPath
        {
            get { return _importFilesPath; }
            set
            {
                if (_importFilesPath == value)
                {
                    return;
                }

                _importFilesPath = value;
            }
        }

        public SettingsViewModel()
        { 
            // Initialize the members
            _appConfig = new AppConfiguration();    
            _utils = new Utils();
            _event = new Eventlog();    
            _eventlogData = new DataView();
            _importFilesPath = "";

            this.InitializeGUI();
        }
        
        private void InitializeGUI()
        {
            //Fill the eventlog grid
            LoadEventlog();

            _importFilesPath = _appConfig.ImportPath;
        }
    }
}

因此,我的问题是,如何将我的UserControl与相应的ViewModel连接起来?我是AvaloniaUI的新手,但我来自WPF,这种方法(给每个UserControl提供自己的ViewModel)都能工作。也许我是在监督一些基本的事情。

谢谢你的每一个回答

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-25 13:42:15

如果您使用MVVM模板创建了您的应用程序,那么您应该有一个名为ViewLocator的文件,它已经将视图模型与相应的视图“连接”起来。然后,如果要在主窗口中显示设置,可以向MainWindowViewModel添加一个属性。

代码语言:javascript
运行
复制
public SettingsViewModel Settings { get; }

并将其绑定到MainWindow中。

代码语言:javascript
运行
复制
<ContentControl Content="{Binding Settings}"/>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74194585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档