I have folder browser dialog that is bound to the SetterName
private void OnClick(object sender, RoutedEventArgs e)
{
var dialog = new FolderBrowserDialog();
var result = dialog.ShowDialog();
if (result == DialogResult.OK && AssociatedObject.DataContext != null)
{
var propertyInfo = AssociatedObject.DataContext.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public)
.Where(p => p.CanRead && p.CanWrite)
.First(p => p.Name.Equals(SetterName));
string dirName = new DirectoryInfo(dialog.SelectedPath).Name;
FolderName = dirName;
_fileName = System.IO.Path.GetFileName(dirName);
FileName = _fileName;
propertyInfo.SetValue(AssociatedObject.DataContext, dialog.SelectedPath, null);
}
}
And I have the properties set in the ViewModel
public class CommonUseWindowViewModel:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Model New { get; set; }
public ICommand Build { get; set; }
public CommonUseWindowViewModel()
{
Build = new DelegateCommand(ClickedMethod);
}
protected async void ClickedMethod()
{
IsEnabled = false;
var gdbName = FolderName;
var styleFol = StyleName;
var envArray = await QueuedTask.Run(() => Geoprocessing.MakeEnvironmentArray(overwriteoutput: true));
var valueArray = await QueuedTask.Run(() => Geoprocessing.MakeValueArray(gdbName, styleFol));
string toolPath = #"c:\staging\ProBaseMapBuilder\BasemapBuilder.tbx\BasemapCreator";
var gpresult1 = await QueuedTask.Run(() => Geoprocessing.ExecuteToolAsync(toolPath, valueArray, envArray));
MessageBox.Show("All Layers Have Been Added to The Map");
IsEnabled = true;
}
private bool _isEnabled = true;
public bool IsEnabled
{
get { return _isEnabled; }
set
{
_isEnabled = value;
OnPropertyChanged("IsEnabled");
}
}
private string _folderName;
public string ShortenedFolderName => Path.GetFileName(_folderName);
public string FolderName
{
get { return _folderName; }
set
{
_folderName = value;
OnPropertyChanged("FolderName");
OnPropertyChanged(nameof(ShortenedFolderName));
}
}
private string _styleName;
public string ShortenedStyleName => Path.GetFileName(_styleName);
public string StyleName
{
get { return _styleName; }
set
{
_styleName = value;
OnPropertyChanged("StyleName");
OnPropertyChanged(nameof(ShortenedStyleName));
}
}
private void OnPropertyChanged(string propertyname)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyname));
}
private void OnPropertyChanged(PropertyChangedEventArgs args)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, args);
}
}
I would like to move the properties to a Model class but when I do the folder dialog raises an error that there is no matching setter name. I think the problem comes not knowing how to bind this to the view. I am wondering if I am referencing namespace correctly in the view
xmlns:FolderDialog="clr-namespace:BasemapCreator.Behaviors"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:Behaviors="clr-namespace:ArcGIS.Desktop.Internal.Framework.Behaviors;assembly=ArcGIS.Desktop.Framework" x:Class="BasemapCreator.CommonUseWindow"
xmlns:DataContext="clr-namespace:BasemapCreator.Models"
<TextBox x:Name="gdbName" HorizontalAlignment="Left" Height="30" Margin="56,29,0,0" Text="{Binding Model.FolderName, Mode=TwoWay}" VerticalAlignment="Top" Width="282" AllowDrop="True" Visibility="Hidden">
When I add the Model.FolderName to the text box binding I get the error coming from folder dialog.
here is my model
namespace BasemapCreator.Models
{
public class Model:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _isEnabled = true;
public bool IsEnabled
{
get { return _isEnabled; }
set
{
_isEnabled = value;
OnPropertyChanged("IsEnabled");
}
}
private string _folderName;
public string ShortenedFolderName => Path.GetFileName(_folderName);
public string FolderName
{
get { return _folderName; }
set
{
_folderName = value;
OnPropertyChanged("FolderName");
OnPropertyChanged(nameof(ShortenedFolderName));
}
}
private string _styleName;
public string ShortenedStyleName => Path.GetFileName(_styleName);
public string StyleName
{
get { return _styleName; }
set
{
_styleName = value;
OnPropertyChanged("StyleName");
OnPropertyChanged(nameof(ShortenedStyleName));
}
}
private void OnPropertyChanged(string propertyname)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyname));
}
private void OnPropertyChanged(PropertyChangedEventArgs args)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, args);
}
}
}
I am using a viewmodel created from a button click
Button Class
namespace BasemapCreator
{
internal class ShowWindow : Button
{
private CommonUseWindow _dlg = null;
protected override void OnClick()
{
if (_dlg != null) return;
_dlg = new CommonUseWindow();
_dlg.Closing += ProWin_Closing;
_dlg.Owner = FrameworkApplication.Current.MainWindow;
_dlg.Show();
}
void ProWin_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
_dlg = null;
}
}
}
In the code behind for the view I have the DataContext set to the viewModel, can I change it and still use the viewmodel?
using ArcGIS.Desktop.Framework.Controls;
using BasemapCreator.ViewModels;
using System;
using System.Windows;
using BasemapCreator.Models;
namespace BasemapCreator
{
/// <summary>
/// Interaction logic for CommonUseWindow.xaml
/// </summary>
public partial class CommonUseWindow : ProWindow
{
private CommonUseWindowViewModel _vm = new CommonUseWindowViewModel();
public CommonUseWindow()
{
InitializeComponent();
this.DataContext = _vm;
}
}
}
Here is the code behind for the view,
In your MainWindow code behind...
using YourProjectName.FolderName;
Then in the MainWindow constructor
InitializeComponent();
//usually this is the only code behind in a view
DataContext = new ClassName();
Related
The code below executes fine when MyActionFunc is called but not when the function is in another class. MessageBox displays the correct string but it is not shown on view. What I am missing?
class ViewModel : INotifyPropertyChanged
{
public MyCommand ActionCommand
{
get;
set;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyname = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
public ViewModel()
{
ActionCommand = new MyCommand();
ActionCommand.CanExecuteFunc = obj => true;
// ActionCommand.ExecuteFunc = MyActionFunc;
ActionCommand.ExecuteFunc = MyClass.MyActionFunc;
}
private string myname;
public string myName
{
get => myname;
set { myname = value;; OnPropertyChanged(); }
}
public void MyActionFunc(object parameter)
{
myName = "Fred";
}
}
class MyClass
{
public static void MyActionFunc(object parameter)
{
ViewModel name = new ViewModel();
name.myName = "Fred";
MessageBox.Show(name.myName);
}
}
... and the binding to the Textbox
<TextBox Name="textBox" Grid.Column="1" Grid.Row="1" Text="{Binding Path=myName, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
I created a MTObservableCollection class that derives from ObservableCollection for multithreading. This is how the function looks like:
public class MTObservableCollection<T> : ObservableCollection<T> where T: LogClassa
{
public MTObservableCollection() { }
public override event NotifyCollectionChangedEventHandler CollectionChanged;
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventHandler CollectionChanged = this.CollectionChanged;
if (CollectionChanged != null)
foreach (NotifyCollectionChangedEventHandler nh in CollectionChanged.GetInvocationList())
{
DispatcherObject dispObj = nh.Target as DispatcherObject;
if (dispObj != null)
{
Dispatcher dispatcher = dispObj.Dispatcher;
if (dispatcher != null && !dispatcher.CheckAccess())
{
dispatcher.BeginInvoke(
(Action)(() => nh.Invoke(this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))),
DispatcherPriority.DataBind);
continue;
}
}
nh.Invoke(this, e);
}
}
}
And here is my LogClass
public class LogClass : INotifyPropertyChanged
{
private string timestamp;
private string title;
private string message;
private MTObservableCollection<LogClass> childRowLog = new MTObservableCollection<LogClass>();
public string TimeStamp
{
get { return timestamp; }
set
{
if( timestamp != value )
{
timestamp = value;
OnPropertyChanged("TimeStamp");
}
}
}
public string Title
{
get { return title; }
set
{
if( title!= value )
{
title= value;
OnPropertyChanged("Title");
}
}
}
public string Message
{
get { return message; }
set
{
if( message!= value )
{
message= value;
OnPropertyChanged("Message");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
So if I have two MTObservableCollection collections and I add elements to one of them such as:
public static MTObservableCollection<LogClass> parentLogCollection = new MTObservableCollection<LogClass>();
public MTObservableCollection<LogClass> childLogCollection = new MTObservableCollection<LogClass>();
childLogClass.Add( new LogClass { TimeStamp = "time", Title = "title", Message = "message" } );
Now if I want to add childLogCollection to parentLogCollection, I would do this:
parentLogCollection = new MTObservableCollection<LogClass>(childLogCollection);
I would think I need to create an implementation within the MBObservationCollection class which should be
public MTObservableCollection(MTObservableCollection<T> collection)
{
}
I just don't know what to input into it, how would I perform it?
Try changing the parent of your MTObservableCollection like this:
public MTObservableCollection(MTObservableCollection<T> collection) : base(collection)
{
}
XAML Button:
<Button Content="Test Connection" Name="btnTestConnection" Command="{Binding Path=TestCommand}" CommandParameter="{Binding ElementName=someObject}"/>
View Model:
public ICommand TestCommand
{
get;
internal set;
}
private bool CanExecuteTestCommand()
{
return !String.IsNullOrEmpty(txtControl);
}
private void CreateTestCommand()
{
TestCommand = new TestCommand(TestExecute);
}
public void TestExecute(object parameter)
{
//do stuff with parameter
obj.TestConnection(parameter);
}
I would like to point out that CreateTestCommand() is called in my VM constructor.
And finally, my implementation of TestCommand:
class TestCommand : ICommand
{
private Action<object> execute;
private Predicate<object> canExecute;
private event EventHandler CanExecuteChangedInternal;
public TestCommand(Action<object> execute)
: this(execute, DefaultCanExecute)
{
}
public TestCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}
if (canExecute == null)
{
throw new ArgumentNullException("canExecute");
}
this.execute = execute;
this.canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
this.CanExecuteChangedInternal += value;
}
remove
{
CommandManager.RequerySuggested -= value;
this.CanExecuteChangedInternal -= value;
}
}
public bool CanExecute(object parameter)
{
return this.canExecute != null && this.canExecute(parameter);
}
public void Execute(object parameter)
{
this.execute(parameter);
}
public void OnCanExecuteChanged()
{
EventHandler handler = this.CanExecuteChangedInternal;
if (handler != null)
{
handler.Invoke(this, EventArgs.Empty);
}
}
public void Destroy()
{
this.canExecute = _ => false;
this.execute = _ => { return; };
}
private static bool DefaultCanExecute(object parameter)
{
return true;
}
}
I set a breakpoint in CreateTestCommand and it looks like it's configured properly:
But when I click on btnTestConnection, nothing happens. TestExecute in my View Model isn't called (which calls TestConnection on the actual model). I must be missing something, but I can't for the life of me figure out what...
EDIT Including the rest of my view model;
class FormProcessorViewModel
{
FormProcessorModel obj;
public FormProcessorViewModel()
{
obj = new FormProcessorModel();
CreateTestCommand();
}
public FormProcessorViewModel(string server, string database, string username, bool specifyDateRange, DateTime startDate, DateTime endDate, string operation, string preprocessed, string processed, string failed) :this()
{
txtServer = server;
txtDatabase = database;
txtUsername = username;
chkSpecifyDateRange = specifyDateRange;
dpStartDate = startDate;
dpEndDate = endDate;
txtOperation = operation;
txtPreprocessed = preprocessed;
txtProcessed = processed;
txtFailed = failed;
}
public ICommand TestCmd
{
get;
internal set;
}
private bool CanExecuteTestCommand()
{
return !String.IsNullOrEmpty(txtUsername);
}
private void CreateTestCommand()
{
TestCmd = new TestCommand(TestExecute);
}
private void TestExecute(object parameter)
{
var passwordBox = parameter as PasswordBox;
var password = passwordBox.Password;
obj.TestConnection(password);
}
}
I left out all the properties that get set in the second constructor just because they don't really do anything but refer to the corresponding values on the model object.
EDIT
View Model
class ViewModel : INotifyPropertyChanged
{
private string _test;
public string TestValue
{
get { return _test; }
set { _test = value; RaisePropertyChanged("TestValue"); }
}
public ICommand MyCommand { get; internal set; }
public ViewModel()
{
TestValue = "Test";
CreateTestCommand();
}
private void CreateTestCommand()
{
MyCommand = new TestCommand(ExecuteButton);
}
private void ExecuteButton(object obj)
{
TestValue = "Cool";
}
public event PropertyChangedEventHandler PropertyChanged;
void RaisePropertyChanged(string propName)
{
var pc = PropertyChanged;
if (pc != null)
{
pc(this, new PropertyChangedEventArgs(propName));
}
}
}
Xaml
<Button Content="{Binding TestValue}" Command="{Binding Path=MyCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
Xaml Code behind.
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
I'm new to WPF and I can't get my grid to auto-refresh when some property changes.
Only thing I achieved - auto-refresh on element adding.
Here is my code:
public partial class MainWindow : Window
{
private Model model;
public MainWindow()
{
InitializeComponent();
model = new Model();
MyGrid.ItemsSource = model.Content;
}
private void MyButton_Click(object sender, RoutedEventArgs e)
{
MyGrid.Items.Refresh();
}
}
public class Model
{
public ObservableCollection<Single> Content;
private Random r;
private Action action;
private static object _syncLock = new object();
public Model()
{
Content = new ObservableCollection<Single>();
r = new Random();
action = new Action(process);
action.BeginInvoke(null,null);
BindingOperations.EnableCollectionSynchronization(Content, _syncLock);
}
private void process()
{
while (true)
{
Content.Add(new Single { Name = "name" });
Content[r.Next(0,Content.Count())].Name = "rename" + r.Next(1,100);
Thread.Sleep(1000);
}
}
}
public class Single : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
name = value;
RaisePropertyChanged(Name);
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Make you model variable notifyable and this will work for you.
Try Changing the above code with.
public partial class Window1 : Window, INotifyPropertyChanged
{
private Model model;
public Model ModelData
{
get { return model; }
set
{
model = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("ModelData"));
}
}
public Window1()
{
this.InitializeComponent();
ModelData = new Model();
MyGrid.ItemsSource = ModelData.Content;
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Model
{
public ObservableCollection<Single> Content { get; set; }
private Random r;
private static object _syncLock = new object();
public Model()
{
Content = new ObservableCollection<Single>();
Content.Add(new Single { Name = "name" });
r = new Random();
// BindingOperations.EnableCollectionSynchronization(Content, _syncLock);
DispatcherTimer t = new DispatcherTimer();
t.Interval = new TimeSpan(2000);
t.Tick += new EventHandler(t_Tick);
t.Start();
}
void t_Tick(object sender, EventArgs e)
{
App.Current.Dispatcher.BeginInvoke(new Action(() =>
{
if (Content.Count <= 100)
Content.Add(new Single { Name = "name" });
Content[r.Next(0, Content.Count())].Name = "rename" + r.Next(1, 100);
}));
}
}
public class Single : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
name = value;
RaisePropertyChanged("Name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Xaml
<DataGrid Name="MyGrid" AutoGenerateColumns="False" Margin="246,175,0,0">
<DataGrid.Columns>
<DataGridTextColumn Header="Names" Binding="{Binding Name }" />
</DataGrid.Columns>
</DataGrid>
The problem was here:
public string Name
{
get { return name; }
set
{
name = value;
RaisePropertyChanged(Name);
}
}
Had to use
RaisePropertyChanged("Name");
Simple and stupid..
I have a MainPage.xaml where is ListBox and Button. When I click on the button then MainPage is navigated to AddPage.xaml. This page is for adding new items, there are two TextBoxes and submit Button. When I click on that submit Button,then data from TextBoxes are saved to XML file and then is called GoBack().
I need to refresh ListBox in my MainPage.xaml when Im going back from AddPage.xaml, but it doesnt work automaticly. How can I do that?
My MainViewModel.cs
public class MainViewModel : INotifyPropertyChanged
{
public ObservableCollection<Context> Contexts { get; private set; }
public MainViewModel()
{
this.Contexts = new ObservableCollection<Context>();
}
public bool IsDataLoaded
{
get;
private set;
}
public void LoadData()
{
try
{
var file = IsolatedStorageFile.GetUserStoreForApplication();
XElement xElem;
using (IsolatedStorageFileStream read = file.OpenFile("contexts.xml", FileMode.Open))
{
xElem = XElement.Load(read);
}
var contexts = from context in xElem.Elements("Context")
orderby (string)context.Element("Name")
select context;
foreach (XElement xElemItem in contexts)
{
Contexts.Add(new Context
{
Name = xElemItem.Element("Name").Value.ToString(),
Note = xElemItem.Element("Note").Value.ToString(),
Created = xElemItem.Element("Created").Value.ToString()
});
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.IsDataLoaded = true;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
and Context.cs
public class Context : INotifyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
NotifyPropertyChanged("Name");
}
}
}
private string _note;
public string Note
{
get
{
return _note;
}
set
{
if (value != _note)
{
_note = value;
NotifyPropertyChanged("Note");
}
}
}
private string _created;
public string Created
{
get
{
return _created;
}
set
{
if (value != _created)
{
_created = value;
NotifyPropertyChanged("Created");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
You'll need to tell the main page that there is new data to reload.
At it's simplest, something like this:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
{
(this.DataContext as MainViewModel).LoadData();
}
}
Tip: You aren't raising a property changed notification for your View Model's properties.
On the load event of the MainPage, call LoadData. You should also clear the observable collection when you call the LoadData method before adding anything to it because simply loading the data will cause duplicate entries in your collection.