标签: mvvm-light

找不到名为“ViewModelLocator”的资源异常

我试图通过在 App.xaml 中将其声明为资源来使用 ViewModelLocator。它是一个非常简单的类,如下所示:

public class ViewModelLocator
    {
        public ShellViewModel ShellPage
        {
            get
            {
                return new ShellViewModel();
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

App.xaml 文件如下:

<Application x:Class="SomeNamespace.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:SomeNamespace.ViewModels">
    <Application.Resources>
         <vm:ViewModelLocator x:Key="ViewModelLocator" />
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

App.xaml.cs 如下:

public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            var view = new ShellView();
            Current.MainWindow = view;
            Current.MainWindow.Show();            
        }
    }
Run Code Online (Sandbox Code Playgroud)

ShellView.xaml 如下:

<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="SomeNamespace.ShellView"
        Title="MainWindow" 
        Height="350" 
        Width="525" 
        ResizeMode="NoResize" 
        MinWidth="700" 
        MinHeight="700"
        DataContext="{Binding ShellPage, Source={StaticResource ViewModelLocator}}"
        >
    <Grid>
        <TextBlock Text="{Binding …
Run Code Online (Sandbox Code Playgroud)

wpf mvvm mvvm-light viewmodellocator

1
推荐指数
1
解决办法
5822
查看次数

为什么不触发 DataGrid 内按钮的单击事件?

我正在使用 MVVM 模式和 MVVM Light 将事件转换为命令,在我的 XAML 中,我有以下代码:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Content="+" Padding="0,0,0,0" Height="Auto">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <i:InvokeCommandAction Command="{Binding MyCommand}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)

在我的视图模型中,我有以下代码:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Content="+" Padding="0,0,0,0" Height="Auto">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <i:InvokeCommandAction Command="{Binding MyCommand}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)

但是点击事件不会被触发。但是,我对应用程序中的每个按钮都使用这种方式,当按钮进入用户控件或窗口时,将触发该事件。

谢谢。

wpf datagrid button mvvm mvvm-light

1
推荐指数
1
解决办法
1500
查看次数

通过在 MVVM Light 中使用数据绑定,在值为空时使 TextBox 边框变为红色

当值为空时,我可以将 TextBox 设为红色,但只有在我单击按钮之后。但是如何在不单击按钮的情况下使其为空时变为红色?

我正在使用 MVVM Light,它与“TwoWay”有关吗?或者 RaisePropertyChanged 或 OnpropertyChanged?

这是我的 xaml 中的一个 TextBox:

 <TextBox Text="{Binding SelectedPerson.FirstName, Mode=TwoWay}"
                 Width="200"
                 HorizontalAlignment="Left"
                 Background="Grey"
                 Foreground="White"
                 BorderThickness="1"
                 BorderBrush="{Binding Color, Mode=TwoWay}" />
Run Code Online (Sandbox Code Playgroud)

这是我的 ViewModel 上的代码:

 private SolidColorBrush _Color;
    public SolidColorBrush Color
    {
        get
        {
            return _Color;
        }
        set
        {
            _Color = value;
            RaisePropertyChanged("Color");
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后这是按钮命令:

 public RelayCommand UpdatePerson
    {
        get
        {
            return new RelayCommand(async () =>
            {
                Color = new SolidColorBrush(Colors.Red);
                //....      
            });
        }
    }
Run Code Online (Sandbox Code Playgroud)

所以我想要的是,当 FirstName TextBox 为空时,边框变为红色,无需单击按钮。

 private Person _SelectedPerson;
    public Person SelectedPerson …
Run Code Online (Sandbox Code Playgroud)

c# data-binding xaml mvvm-light

1
推荐指数
1
解决办法
2485
查看次数

如何将 xBind 与 ViewModelLocator 结合使用?

想要使用新的x:Bind编译时绑定 MVVMLight 和ViewModelLocator类。

如何更改它以使用 x:Bind?

DataContext="{Binding Login, Source={StaticResource Locator}}"
Run Code Online (Sandbox Code Playgroud)

源似乎不受支持,因此失败:

DataContext="{x:Bind Path=Login, Source={StaticResource Locator}}"
Run Code Online (Sandbox Code Playgroud)

又怎样?

c# mvvm-light win-universal-app

1
推荐指数
1
解决办法
631
查看次数

为“System.Windows.StaticResourceExtension”提供值引发异常

我正在尝试通过 Converter 绑定 WPF 中窗口的可见性。我收到错误。系统.Windows.StaticResourceExtension 系统.Windows.StaticResourceExtension

我在下面提供我的代码。我的视图是 在此处输入图像描述

<Window x:Class="UI.ChildWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    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:local="clr-namespace:UI"
    xmlns:UtilityValue="clr-namespace:UI.Utility"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"        
    mc:Ignorable="d"
    Title="ChildWindow" Height="70" Width="400" WindowStartupLocation="CenterScreen" WindowStyle="None" 
    Visibility="{Binding WindowVisibility, Converter={StaticResource VisibilityConverter}, Mode=TwoWay}">    
<Window.Resources>
    <UtilityValue:TextInputToVisibilityConverter x:Key="TextInputToVisibilityConverter"></UtilityValue:TextInputToVisibilityConverter>
    <UtilityValue:EventToCommandBehavior x:Key="CommandBehavior"></UtilityValue:EventToCommandBehavior>
    <SolidColorBrush x:Key="brushWatermarkBackground" Color="White" />
    <SolidColorBrush x:Key="brushWatermarkForeground" Color="LightSteelBlue" />
    <SolidColorBrush x:Key="brushWatermarkBorder" Color="Indigo" />
    <UtilityValue:BooleanToVisibilityConverter x:Key="VisibilityConverter"></UtilityValue:BooleanToVisibilityConverter>      
    <Style x:Key="EntryFieldStyle" TargetType="Grid" >
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Margin" Value="2" />
    </Style>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

我的视图模型如下:

区域窗口可见性

    private bool _windowVisibility=true;
    public bool WindowVisibility
    {
        get { return _windowVisibility; }
        set …
Run Code Online (Sandbox Code Playgroud)

wpf xaml mvvm mvvm-toolkit mvvm-light

1
推荐指数
1
解决办法
1万
查看次数

MVVM在模型中命令是一种不好的做法?

我想知道,将命令放入模型是MVVM中的不良做法.例如,我有ListBoxImageButton.当我点击时Button我需要在浏览器中打开网址.所以,我的代码看起来像这样:

<ListBox ItemSource="{Binding Items">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Image Source={Binding ImageSource} />
                <Button Content="Open url" Command={Binding OpenUrlCommand}"/>
            </StackPanel>
        </DataTemplate>
    </Listbox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

视图模型:

class MainViewModel:BaseViewModel
{
    public ObservableCollection<Model> Items {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

模型:

class Model
{
    public string ImageSource {get;set;}

    public string Url {get;set;}

    public ICommand OpenUrlCommand {get;set;}

    public Model()
    {
       OpenUrlCommand = new RelayCommand(openUrl);
    }

    public void openUrl()
    {
        Process.Start(Url); //Open url in browser
    }
}
Run Code Online (Sandbox Code Playgroud)

它是确定,或者我应该移动OpenUrlCommandMainViewModel

c# wpf mvvm mvvm-light

1
推荐指数
1
解决办法
937
查看次数

奇怪的行为 onCanExecute RelayCommand MVVMLight 5+

我正在将一个工具从 MVVM Light 4.0.3 迁移到 5.4.1,我发现最新的RelayCommand实现有一个非常奇怪的问题。

这是V4.0.3 中的旧实现:

图像1

图像2

这是V5.4.1 中的最新实现:

img4

img3

在我能够使用变量通过以下代码定义canExecute行为(启用按钮)之前:

public ICommand GetNewItemsFromDB { get; private set; }

private bool _IsActive;
public bool IsActive
{
    get
    {
        return _IsActive;
    }
    set
    {
        if (_IsActive != value)
        {
            _IsActive = value;
            this.RaisePropertyChanged(() => IsActive);
        }
    }
}

GetNewItemsFromDB = new RelayCommand(GetDataFromDB, () => { return IsActive == false; });

private void GetDataFromDB()
{
    IsActive = true;
}
Run Code Online (Sandbox Code Playgroud)

之前的代码能够在 MVVM Light 4.0.3 中启用按钮而没有任何问题;然而,在最新的实现中总是被禁用,我添加了一些更改,因为有一个新的 …

c# wpf relaycommand mvvm-light .net-4.5

1
推荐指数
1
解决办法
343
查看次数

RelayCommand.CanExecute不在UI中更新IsEnabled

我有一个Windows Phone 8应用程序,我有一个RelayCommand名为的实例DiscoverExpansionModulesCommand.我有一个Command属性绑定的按钮DiscoverExpansionModulesCommand.首次加载应用时,会正确启用或禁用该按钮.但是,当在页面上并且我想要更改命令是否可以执行时,方法会CanExecuteDiscoverExpansionModulesCommand()正确触发并返回正确的true或false值,但按钮不会反映它.为什么按钮没有更新它的UI?我在这里找到了另一篇关于这个问题的文章http://social.msdn.microsoft.com/Forums/en-US/silverlightarchieve/thread/48a341e4-f512-4c33-befd-b614404b4920/

我的ViewModel:

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using MAL.Portable.Commands;
using MAL.Portable.Message;
using MAL.Portable.Model;
using System;
using System.Collections.Generic;
using System.Windows.Input;

namespace MAL.Portable.ViewModel
{
    public class SettingsViewModel : ViewModelBase
    {
        // Define an observable collection property that controls can bind to.
        private List<Setting> settings;
        private String controllerUrl;
        private String controllerPort;
        private String temperature;
        private Wifi wifi;
        private Boolean connected;
        private Boolean checkingConnection;


        public SettingsViewModel()
        {
            DiscoverExpansionModulesCommand = new …
Run Code Online (Sandbox Code Playgroud)

button relaycommand mvvm-light windows-phone

0
推荐指数
1
解决办法
4019
查看次数

ProgressRing IsActive属性未更新

我正在编写一个小型UWP项目,也使用MVVM模式(MVVM Light框架)进行研究,并在许多场景和控件中遇到了绑定问题.

我已经努力将其中一个分离成这里提供的迷你示例项目.

ProgressRing控件的IsActive属性对ViewModel方面的更新没有做出反应,即使它具有:

  • INotifyPropertyChanged已实现
  • 绑定模式明确设置为TwoWay绑定
  • UpdateSourceTrigger显式设置为PropertyChanged

MainPage.xaml中

<Page
    x:Class="TestProgressRing.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:core="using:Microsoft.Xaml.Interactions.Core"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:i="using:Microsoft.Xaml.Interactivity"
    xmlns:local="using:TestProgressRing"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Width="491.282"
    Height="492.308"
    DataContext="{Binding MainPageViewModel, Source={StaticResource ResourceKey=Locator}}"
    mc:Ignorable="d">
    <Grid
        Width="500"
        Height="800"
        Margin="0,0,-8.667,-307.667"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="3*" />
        </Grid.RowDefinitions>
        <Button
            Grid.Row="0"
            Width="250"
            Height="50"
            Margin="0"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Content="Boom">
            <i:Interaction.Behaviors>
                <core:EventTriggerBehavior EventName="Click">
                    <core:InvokeCommandAction Command="{Binding ClickCommand}" />
                </core:EventTriggerBehavior>
            </i:Interaction.Behaviors>
        </Button>
        <ProgressRing
            Grid.Row="1"
            Width="500"
            Height="500"
            Margin="0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            IsActive="{Binding IsLoading, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
            Visibility="Visible" />
    </Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)

MainPageViewModel.cs

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command; …
Run Code Online (Sandbox Code Playgroud)

c# xaml mvvm mvvm-light uwp

0
推荐指数
1
解决办法
461
查看次数

WPF缩放画布中心鼠标位置

<utils:ScrollViewer x:Name="ImageViewer" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Row="2"                                                                   
                                       CurrentHorizontalOffset="{Binding ScrollHorizontalValue, Mode=TwoWay}"
                                       CurrentVerticalOffset="{Binding ScrollVerticalValue, Mode=TwoWay}"                                        
                                       >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="PreviewMouseWheel">
                            <cmd:EventToCommand Command="{Binding MouseWheelZoomCommand}" PassEventArgsToCommand="True"/>
                        </i:EventTrigger>
                        <i:EventTrigger EventName="ScrollChanged">
                            <cmd:EventToCommand Command="{Binding ScrollChangedCommand}" PassEventArgsToCommand="True"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <Grid Background="{StaticResource ThatchBackground}" RenderTransformOrigin="0.5,0.5">
                        <ItemsControl ItemsSource="{Binding CanvasItems}" ItemTemplate="{StaticResource templateOfROI}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <Canvas x:Name="BackPanel"
                                        Width="{Binding DataContext.ImageWidth, ElementName=MainGrid}" 
                                        Height="{Binding DataContext.ImageHeight, ElementName=MainGrid}"
                                        ClipToBounds="True">
                                        <Canvas.Background>
                                            <ImageBrush x:Name="BackImage"
                                                        ImageSource="{Binding DataContext.SelectedImage.Path, ElementName=MainGrid}"/>
                                        </Canvas.Background>

                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="MouseRightButtonDown">
                                                <cmd:EventToCommand Command="{Binding DataContext.MouseRightCommand, ElementName=MainGrid}"/>
                                            </i:EventTrigger>
                                            <i:EventTrigger EventName="MouseLeftButtonDown">
                                                <cmd:EventToCommand Command="{Binding DataContext.MouseMoveStartCommand, ElementName=MainGrid}" PassEventArgsToCommand="True"/>
                                            </i:EventTrigger>
                                            <i:EventTrigger EventName="MouseMove">
                                                <cmd:EventToCommand Command="{Binding DataContext.MouseMovingCommand, ElementName=MainGrid}" PassEventArgsToCommand="True"/>
                                            </i:EventTrigger>
                                            <i:EventTrigger …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml mvvm mvvm-light

0
推荐指数
2
解决办法
1857
查看次数