WPF FolderBrowserDialog using MVVM(without code behind) - c#

Explanation:
I have to get FolderBrowserDialog Box once I click the browse button. And In FolderBrowserDialog Box,if I select any folder and click ok,that particular folderpath along with foldername should be shown in the textbox which is beside the browse button....But I didn't get anything once I click Browse button.
Please check my code and correct me...
View.xaml :
<Window.... xmlns:VM="clr-namespace:myproject.myViewModel"
... >
<Window.DataContext><VM:myViewModel/>
<Grid>...
<TextBlock Text="Folder to save files" VerticalAlignment="Center" />
<TextBox Text="{Binding Path=FoldernameWithPath , UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Height="26" IsReadOnly="True" VerticalContentAlignment="Center" Width="150" />
<Button Content="Browse" Height="26" VerticalAlignment="Bottom" MinWidth="45" Command="{Binding OpenFolderCommand}" />
</Grid>
</window>
ViewModel.cs
public ICommand OpenFolderCommand
{
get => new RelayCommand(a => this.OpenFolder(), p => CanOpenFolder());
}
private string _foldernameWithPath;
public string FoldernameWithPath
{
get { return _foldernameWithPath; }
set
{
if (value == _foldernameWithPath)
{
return;
}
else
{
_foldernameWithPath = value;
OnPropertyChanged("FoldernameWithPath");
}
}
}
public bool CanOpenFolder()
{
return true;
}
private void OpenFolder()
{
FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && OpenFolderCommand.CanExecute(openFolderDialog.SelectedPath))
{
OpenFolderCommand.Execute(openFolderDialog.SelectedPath);
FoldernameWithPath = openFolderDialog.SelectedPath;
}
}

You should not call the command from the delegated method (OpenFolder()). The command does nothing else than to execute the delegated method when Execute is called, which is done automatically when you click the button.
Also ICommand.CanExecute() is typically called automatically by WPF itself and based on the result in only enables, resp disables the button. You rarely call the CanExecute by yourself in ViewModel. In your case, you want the button always enabled, so you can skip CanExecute, or use p => true expression.
This should work
xaml:
<TextBox Text="{Binding Path=FoldernameWithPath}" IsReadOnly="True" />
<Button Content="Browse" Command="{Binding OpenFolderCommand}" />
viewmodel:
public ICommand OpenFolderCommand {get;} = new RelayCommand(p => OpenFolder());
private string _foldernameWithPath;
public string FoldernameWithPath
{
get { return _foldernameWithPath; }
set
{
if (value == _foldernameWithPath) return
_foldernameWithPath = value;
OnPropertyChanged("FoldernameWithPath");
}
}
public void OpenFolder()
{
FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FoldernameWithPath = openFolderDialog.SelectedPath;
}
}

Do the following changes,
In View.xaml
<Button Content="Browse" Command="{Binding OpenFolderCommand}"/>
In ViewModel.cs
public bool CanOpenFolder()
{
return true;
}
private void OpenFolder()
{
FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && OpenFolderCommand.CanExecute(openFolderDialog.SelectedPath))
{
//OpenFolderCommand.Execute(openFolderDialog.SelectedPath);
FoldernameWithPath = openFolderDialog.SelectedPath;
}
}

I would write the command like this:
public ICommand OpenFolderCommand { get; private set; }
public MyViewModel()
{
this.OpenFolderCommand = new RelayCommand(a=> this.OpenFolder(),p=> CanOpenFolder());
}

Related

Adding a character to a tab title when modifying text

i'm working on making a notepad++ equivalent in C# using MVVM design pattern for an university assignment. I've created the tabs successfully but now I have a problem adding the little "*" to the tabname when the content changes from the original and making it disappear upon saving. How can this be implemented ?
Here is the code for the tabcontrol:
<TabControl Margin="10,26,10,10" Grid.Column="2" ItemsSource="{Binding FileTabs}" SelectedIndex="{Binding CurrentSelectedTab}">
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FileTabName}" />
<Button Command="{Binding Close,
RelativeSource={RelativeSource AncestorType={x:Type local:FileMenuCommands}},
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" Width="20" Height="20" Content="X"/>
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<TextBox Text="{Binding FileTabContent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AcceptsReturn="True" AcceptsTab="True" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
The model for the tabfile:
using System;
using System.Collections.Generic;
using System.Text;
namespace Notepad___.Model
{
class FileTabProvider
{
public string FileTabName { get; set; }
public string FileFullPath { get; set; }
public string FileTabContent { get; set; }
public FileTabProvider(string FileTabName, string FileFullPath, string FileTabContent)
{
this.FileTabName = FileTabName;
this.FileFullPath = FileFullPath;
this.FileTabContent = FileTabContent;
}
}
}
Also the two save functions created in the view model of the mainwindow:
private void SaveFile(object parameter)
{
if (FileTabs[CurrentSelectedTab].FileFullPath == "")
SaveAsFile(parameter);
else
File.WriteAllText(FileTabs[CurrentSelectedTab].FileFullPath, FileTabs[CurrentSelectedTab].FileTabContent.ToString());
}
private void SaveAsFile(object parameter)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (saveFileDialog.ShowDialog() == true)
File.WriteAllText(saveFileDialog.FileName, FileTabs[CurrentSelectedTab].FileTabContent.ToString());
}
Implement the INotifyPropertyChanged interface in your view model and change the FileTabName property whenever the FileTabContent property is set. Something like this:
class FileTabProvider : INotifyPropertyChanged
{
private string _originalFileTabName;
private string _fileTabName;
public string FileTabName
{
get { return _fileTabName; }
set { _fileTabName = value; OnPropertyChanged(nameof(FileTabName)); }
}
public string FileFullPath { get; set; }
private string _fileTabContent;
public string FileTabContent
{
get { return _fileTabContent; }
set
{
_fileTabContent = value;
FileTabName += "*";
}
}
public FileTabProvider(string fileTabName, string fileFullPath, string fileTabContent)
{
_fileTabName = fileTabName;
FileFullPath = fileFullPath;
_fileTabContent = fileTabContent;
}
public void Save() => FileTabName = _originalFileTabName;
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Whenever you save, you need to remember to reset the filename:
private void SaveFile(object parameter)
{
var tab = FileTabs[CurrentSelectedTab];
if (tab.FileFullPath == "")
SaveAsFile(parameter);
else
File.WriteAllText(tab.FileFullPath, tab.FileTabContent.ToString());
tab.Save();
}
You can do it this way:
<Grid>
<TabControl>
<TabItem Header="ABC" x:Name="TabItem1" KeyDown="TabItem1_OnKeyDown">
<TabItem.Content>
<Grid>
<TextBox Text="{Binding YourTextProp}" TextChanged="TextBoxBase_OnTextChanged"/>
</Grid>
</TabItem.Content>
</TabItem>
</TabControl>
</Grid>
public MainWindow()
{
InitializeComponent();
}
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
if (TabItem1.Header is string tabItemHeader && !tabItemHeader.Contains("*"))
{
tabItemHeader += "*";
TabItem1.Header = tabItemHeader;
}
}
private void TabItem1_OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control && TabItem1.Header is string tabItemHeader)
{
tabItemHeader = tabItemHeader.Substring(0, tabItemHeader.Length - 1);
TabItem1.Header = tabItemHeader;
}
}

C#-Accessing .xaml created checkbox/listbox

Is there a way to verify if checkbox and a specific item in a listbox has been selected, when it was created in .xaml?
I am trying to access these elements from a different class rather than the ViewModel.
I want to be able to do something as follows;
if (First_CheckBox.Ischecked && LocationBox.SelectedItem == "FirstValue")
{
do something;
}
else
{
do something else;
}
.XAML Code:
<CheckBox x:Name="First_CheckBox" IsChecked="{Binding Check, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Content="Integral "/>
<ListBox x:Name="LocationBox" ItemsSource="{Binding LocationList}" SelectionMode="Single" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" Margin="0,5" Width="100" HorizontalAlignment="Left" BorderThickness="1" Background="#FFF0ECEC">
Current Code:
namespace ValueClearanceCalculator.ViewModels
{
class MainWindowViewModel : CommonBase
{
public delegate void PropertyChangedHandler(object obj);
public static event PropertyChangedHandler MainVMPropertyChanged = delegate { };
public MainWindowViewModel()
{
CalculateAndDrawPatternCommand = new CalculatePatternCommand(this);
}
private public bool _Check;
public bool Check
{
get
{
return _Check;
}
set
{
_Check = value;
RaisePropertyChanged("Check");
_C = _A;
double temp;
bool result = double.TryParse(_C, out temp);
if (result == false) { MessageBox.Show("Can Not Convert A to C"); }
else { FrontPanelVariables.C = temp * 25.4; }
RaisePropertyChanged("C");
_F = ".157";
FrontPanelVariables.F = .157;
RaisePropertyChanged("F");
}
}

Prism 6.2 Binding to Model not ViewModel

I am trying to use Prism in a C# to but I seem to have set it up so that it binds to items in my model and not my viewmodel. It is a short program that is more of a learning tool that anything. When I move the items to the Viewmodel the SetProperty's don't seem to notify the view of the change.
Any thoughts as to how I have this setup backwards?
Model:
namespace XMLValueModifier_Threaded_WPF.Models
{
public class XMLReadFileModel : BindableBase
{
private string _XMLMasterFile2 = "0";
public string XMLGetFileName()
{
if (_XMLMasterFile2 != "1")
{
Microsoft.Win32.OpenFileDialog _XMLMasterFileDialog = new Microsoft.Win32.OpenFileDialog();
_XMLMasterFileDialog.DefaultExt = "xml";
_XMLMasterFileDialog.Filter = "xml Files (*.xml; *.XML) | *.xml; *.XML";
Nullable<bool> result = _XMLMasterFileDialog.ShowDialog();
if (result == true)
{
return _XMLMasterFileDialog.FileName;
}
return "";
}
else
{
return "";
}
}
}
}
ViewModel:
namespace XMLValueModifier_Threaded_WPF.ViewModels
{
public class MainDialogueViewModel : BindableBase
{
private string _XMLMasterFile;
public ICommand MasterFileLocation
{
get;
set;
}
public ICommand XMLFileLocation
{
get;
set;
}
public string XMLMasterFile
{
get
{
return _XMLMasterFile;
}
set
{
SetProperty(ref _XMLMasterFile, value);
}
}
private XMLReadFileModel xmlReadFileModel = new XMLReadFileModel();
public MainDialogueViewModel()
{
XMLReadFileModel xmlReadFileModel = new XMLReadFileModel();
Message = "example message";
XMLMasterFile = "example File";
this.MasterFileLocation = new DelegateCommand(chooseFile, canChooseFile);
this.XMLFileLocation = new DelegateCommand(chooseFile, canChooseFile);
}
public void masterfilelocation()
{
MessageBox.Show("i am here");
return;
}
private void chooseFile()
{
XMLMasterFile = xmlReadFileModel.XMLGetFileName();
}
private bool canChooseFile()
{
if (XMLMasterFile != null)
{
return true;
}
else
{
return true;
}
}
}
}
XAML:
<Window x:Class="XMLValueModifier_Threaded_WPF.Views.MainDialogue"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:XMLValueModifier_Threaded_WPF.ViewModels" Width="625" Height="452"
>
<Grid Margin="0,-24,0,-3">
<TextBox x:Name="Textbox1" Text="{Binding MainDialogueViewModel.XMLMasterFile,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="25,120,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="425"/>
<TextBox x:Name="Textbox2" Text="{Binding MainDialogueViewModel.XMLFiles,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="25,188,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="425" RenderTransformOrigin="0.506,1.565"/>
<Label Content="Location of Master XML File" HorizontalAlignment="Left" Margin="25,89,0,0" VerticalAlignment="Top"/>
<Label Content="Location of XML File(s)" HorizontalAlignment="Left" Margin="25,157,0,0" VerticalAlignment="Top"/></GRID>
Assuming you have your DataContext setup correctly to an instance of MainDialogueViewModel; you don't need to include MainDialogueViewModel in your binding. Simply bind to the property name XMLMasterFile. Also keep in mind that if the value isn't different, then nothing will be updated.

How do you get text from a textbox with databinding propertynotifying thingy mvvm light

I have a username and password box.
Underneath it I have a button.
When I click that button I want to analyse what has been put into the username and password box.
How do I do this with mvvm light?
This is where I am:
XAML
...DataContext="{Binding Main, Source={StaticResource Locator}}">...
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0">
<TextBlock HorizontalAlignment="Left" Margin="10,0,0,0" TextWrapping="Wrap" Text="Username" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="72" Margin="0,27,0,0" TextWrapping="Wrap" Text="{Binding Username}" VerticalAlignment="Top" Width="456"/>
<TextBlock HorizontalAlignment="Left" Margin="10,99,0,0" TextWrapping="Wrap" Text="Password" VerticalAlignment="Top"/>
<PasswordBox HorizontalAlignment="Left" Height="72" Margin="0,126,0,0" Password="{Binding Password}" VerticalAlignment="Top" Width="456"/>
<Button Content="Log in" HorizontalAlignment="Center" Margin="167,203,169,0" VerticalAlignment="Top" Command="{Binding LogInCommand}"/>
</Grid>
View Model
public class MainViewModel : ViewModelBase
{
public LoginCredentials LoginCredentials { get; set; }
public ICommand LogInCommand { get; private set; }
public MainViewModel()
{
LoginCredentials = new LoginCredentials();
LogInCommand = new RelayCommand(this.OnLogInCommand);
}
private void OnLogInCommand()
{
string testUsername = Username;
string testPassword = Password;
}
#region Properties
public string Username
{
get { return LoginCredentials.Username; }
set { LoginCredentials.Password = value; }
}
public string Password
{
get { return LoginCredentials.Password; }
set { LoginCredentials.Password = value; }
}
#endregion
}
MainPage.xaml.cs
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
}
what is happening at the moment:
When I click my button, the LogInCommand is run and it fires my method OnLoginCommand. I have put a break point on the testUsername declaration to see if when the button is clicked, the username and password reflect what has been put in; they are both empty. What must I do to make sure these are updated as someone is typing or when the button is pressed or however it works???
I have now spent about 4 weeks learning mvvm and trying to get a simple click event and binding to work. This is simply not making sense... doh. Thanks for any help!
P.S - Is MVVM light too confusing for new comers? the documentation is so.. light on detail. No examples :(
View
Windows Phone doesn't contain "UpdateSourceTrigger=PropertyChanged". You have to use "Explicit" and manually call "UpdateSource" in code behind, otherwise value of TextBox/PasswordBox will be raise when TextBox/PasswordBox lost focus.
And don't forget to set "Mode=TwoWay".
<TextBox
Text="{Binding Path=Username, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
TextChanged="TextBoxTextChanged" />
<PasswordBox
Password="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
PasswordChanged="PasswordBoxPasswordChanged" />
<Button
Command="{Binding Path=LogInCommand}"
Content="Log in" />
View - code behind
private void PasswordBoxPasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox pb = sender as PasswordBox;
if (pb != null)
{
pb.GetBindingExpression(PasswordBox.PasswordProperty).UpdateSource();
}
}
private void TextBoxTextChanged(object sender, TextChangedEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb != null)
{
tb.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
}
ViewModel
Fields
private RelayCommand _logInCommand;
private string _password;
private string _username;
Properties
public bool CanExecuteLogInCommand
{
get
{
return !string.IsNullOrWhiteSpace(this.Username) && !string.IsNullOrWhiteSpace(this.Password);
}
}
public RelayCommand LogInCommand
{
get
{
// or you can create instance in constructor: this.LogInCommand = new RelayCommand(this.ExecuteLogInCommand, () => this.CanExecuteLogInCommand);
return this._logInCommand ?? (this._logInCommand = new RelayCommand(this.ExecuteLogInCommand, () => this.CanExecuteLogInCommand));
}
}
public string Username
{
get { return this._username; }
set
{
// a) shorter alternative -> "True if the PropertyChanged event has been raised, false otherwise"
if (this.Set(() => this.Username, ref this._username, value))
{
// raise CanExecuteLogInCommand
this.LogInCommand.RaiseCanExecuteChanged();
}
// b) longer alternative
//if (value == this._username) { return; }
//this._username = value;
//this.RaisePropertyChanged(() => this.Username);
//this.LogInCommand.RaiseCanExecuteChanged();
}
}
public string Password
{
get { return this._password; }
set
{
if (this.Set(() => this.Password, ref this._password, value))
{
this.LogInCommand.RaiseCanExecuteChanged();
}
}
}
Methods
private void ExecuteLogInCommand()
{
// .... = this.Username;
// .... = this.Password;
}
Check this sample.
To get the View and ViewModel 'linked up' so that they are synchronized, you need to implement INotifyPropertyChanged (Encapsulated in ViewModelBase). i.e.:
private string userName;
public string UserName
{
get { return userName; }
set
{
if (value != userName)
{
userName = value;
RaisePropertyChanged("UserName");
}
}
}

Converting Silverlight to MVVM, a couple of issues

I have a Silverlight project I'm converting to MVVM. It's my first time using the pattern and I'm struggling with something.
So basically I had this in the XAML code behind page:
OpenFileDialog ofd = new OpenFileDialog();
if ((bool)ofd.ShowDialog())
{
_fileName = ofd.File.Name;
FileStream fs = ofd.File.OpenRead();
fileSize = (double)fs.Length;
txtFileName.Text = fileName;
index = 0;
sendData = 0;
byte[] file = new byte[fs.Length];
fs.Read(file, 0, file.Length);
//convertToChunks(file);
prgUpload.Maximum = fileChunks.Count;
prgUpload.Value = 0;
//uploadChunks(index);
}
And I cannot figure out how to wire it up to be able to use that in the model? I assume the viewmodel comes into play, but nothing is working.
Any thoughts?
Here is the work in progress XAML:
<Grid x:Name="LayoutRoot" Width="475" Height="340">
<Canvas Margin="8,8,0,0" Background="White" Height="320" VerticalAlignment="Top" HorizontalAlignment="Left" Width="475">
<Button Width="75" Canvas.Left="380" Canvas.Top="43" Content="Browse" x:Name="btnBrowse" />
<TextBox Canvas.Left="25" IsReadOnly="True" Canvas.Top="43" TextWrapping="Wrap" Width="350" Text="{Binding Path=FileUploadName}" x:Name="txtFileName" />
<ProgressBar Height="10" Width="350" Canvas.Left="25" Canvas.Top="99" x:Name="prgUpload" />
<my:Label Content="Please select a file to upload" Name="lblError" Canvas.Left="25" Canvas.Top="23" RenderTransformOrigin="0.133,-0.063" Width="220"/>
<my:Label x:Name="lblProgress" Canvas.Left="25" Canvas.Top="78" RenderTransformOrigin="0.133,-0.063" Width="220"/>
</Canvas>
</Grid>
Basically I want it to fire after the user selects a file to upload.
If you want to fire a command this would do the work for you
<Button Width="75" Canvas.Left="380" Canvas.Top="43" Content="Browse" x:Name="btnBrowse"
Command={Binding OpenFileCommand} />
in your code behind Constructor for example
partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
this.DataContext=new MainViewModel();
}
}
and in your ViewModel
public ICommand OpenFileCommand { get; set; }
public MainViewModel()
{
OpenFileCommand = new RelayCommand(OpenDialog) { IsEnabled = true };
}
private void OpenDialog()
{
OpenFileDialog ofd = new OpenFileDialog();
if ((bool)ofd.ShowDialog())
{
_fileName = ofd.File.Name;
FileStream fs = ofd.File.OpenRead();
fileSize = (double)fs.Length;
//txtFileName.Text = fileName;// Apply Binding
index = 0;
sendData = 0;
byte[] file = new byte[fs.Length];
fs.Read(file, 0, file.Length);
//convertToChunks(file);
prgUpload.Maximum = fileChunks.Count;
prgUpload.Value = 0;
//uploadChunks(index);
}
}
And the RelayCommand
public class RelayCommand:ICommand
{
private bool _isEnabled;
public bool IsEnabled
{
get { return _isEnabled; }
set
{
if (value != _isEnabled)
{
_isEnabled = value;
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
}
private Action _handler;
public RelayCommand(Action handler)
{
_handler = handler;
}
public bool CanExecute(object parameter)
{
return IsEnabled;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_handler();
}
}
in order to get the filename in your textbox you have to bind the textbox to to the view model. so that it will appear on the UI and also implement INotifyPropertyChanged. Also Look at this it would be helpful Silverlight MVVM

Categories

Resources