У меня есть вопрос о привязках. Я попытался привязать свой второй вид к Viewmodel в пустом приложении с помощью fody.propertychanged, потому что я сделал это в своем проекте, который у меня был, и он не работал.
Итак, я сделал чистый лист и повторил, и это все еще не работает. Я понятия не имею, почему это не работает; это то, что я пробовал до сих пор.
BaseViewModel
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using PropertyChanged;
namespace TestApp
{
[AddINotifyPropertyChangedInterface]
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void SetProperty<T>(ref T storage, T value, string propertyName)
{
storage = value;
RaisePropertyChangedEvent(propertyName);
}
protected void RaisePropertyChangedEvent(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
BrowseViewModel
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using System.Threading;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Specialized;
using System.IO;
namespace TestApp
{
public class BrowseViewModel : BaseViewModel
{
#region public variables
private string _webUrl;
public string WebUrl
{
get => _webUrl;
set
{
//This will change based on how you have implemented your BaseViewModel!
//The method name might be different, or have different parameters!
this.SetProperty(ref _webUrl, value, nameof(WebUrl));
//Call the save file path validation method...
SaveFilePath();
}
}
private string _errorMessage;
public string ErrorMessage
{
get => _errorMessage;
private set
{
//This will change based on how you have implemented your BaseViewModel!
//This method should call NotifyPropertyChange to notify the UI to update...
this.SetProperty(ref _errorMessage, value, nameof(ErrorMessage));
}
}
#endregion
#region Public Commands
public ICommand SaveCommand { get; set; }
#endregion
#region Constructor
public BrowseViewModel()
{
this.SaveCommand = new RelayCommand(SaveFilePath);
}
#endregion
#region Private methods
private void SaveFilePath()
{
if (File.Exists(_webUrl))
{
_errorMessage = "String works";
}
else
{
_errorMessage = "Filen existerar ej";
}
}
#endregion
}
}
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TestApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
BrowseWindow browse = new BrowseWindow();
browse.Show();
browse.DataContext = new BrowseViewModel();
InitializeComponent();
}
}
}
BrowseWindow .xaml
<Window x:Class="TestApp.BrowseWindow"
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:TestApp"
mc:Ignorable="d"
Title="BrowseWindow" Height="450" Width="800">
<Grid>
<Label Content="{Binding errorMessage}" HorizontalAlignment="Left" Margin="10,383,0,0" VerticalAlignment="Top" />
<Button Content="Button" HorizontalAlignment="Left" Margin="684,368,0,0" VerticalAlignment="Top" Width="75" Command="{Binding SaveCommand}"/>
</Grid>
</Window>
BrowseWindow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace TestApp
{
/// <summary>
/// Interaction logic for BrowseWindow.xaml
/// </summary>
public partial class BrowseWindow : Window
{
public BrowseWindow()
{
InitializeComponent();
this.DataContext = new BrowseViewModel();
}
}
}
RelayCommand
using System;
using System.Windows.Input;
namespace TestApp
{
public class RelayCommand : ICommand
{
#region Public events
/// <summary>
/// Eventet som används när <see cref="CanExecute(object)"/> värdet har ändrats
/// </summary>
public event EventHandler CanExecuteChanged = (sender, e) => { };
#endregion
#region Private members
private Action mAction;
#endregion
#region Constructor
/// <summary>
/// Standard Konstruktor
/// </summary>
public RelayCommand(Action action)
{
mAction = action;
}
#endregion
#region Command Methods
/// <summary>
/// Ett action Kommando kan alltid köras
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
mAction();
}
#endregion
};
}