My codes doesn't read the text file. Apparent;y, when I want to read the file, they gave me a "File Not Found!" message box which it is in the if else loop, indicating that the code in the if loop doesn't work. So I need some help with this. And this question is still unanswered. By giving this much codes below, i'm just hoping that it might help you guyz understand what is my problem over here as well, because i'm not good at programming, and I'm just trying co-operate as well. Sorry to those who i've troubled a lot. Every answers you guyz gave meant something, and I really appreciate the help.
CreateTextPage XAML:
<TextBox Margin="0,217,0,338" TextWrapping="Wrap" Text="{Binding FileName, Mode=TwoWay}" HorizontalAlignment="Right" Width="480" IsReadOnly="False" Name="textFileName" />
<TextBox Text="{Binding FileText1, Mode=TwoWay}" HorizontalAlignment="Left" Width="447" Height="378" VerticalAlignment="Top" Name="text1" />
<TextBox Text="{Binding FileText2, Mode=TwoWay}" HorizontalAlignment="Left" Width="447" Height="378" VerticalAlignment="Top" Name="text2"/>
CreateTextPage:
private void Button_Click(object sender, RoutedEventArgs e)
{
AddFileModel model = this.LayoutRoot.DataContext as AddFileModel;
model.SaveFile.Execute(null);
model.FileName = string.Empty;
model.FileText1 = string.Empty;
model.FileText2 = string.Empty;
MessageBox.Show("File saved successfully");
NavigationService.Navigate(new Uri("/CompleteQuestionPage.xaml", UriKind.Relative));
}
ReadFilePage XAML:
<TextBlock HorizontalAlignment="Right" Margin="0,0,-409,-260" TextWrapping="Wrap" VerticalAlignment="Bottom" Width="333" Text="{Binding FileName, Mode=TwoWay}" Name="titleText" />
<TextBlock TextWrapping="Wrap" Height="378" Width="452" Text="{Binding FileText1, Mode=TwoWay}" Name="textBlocky1" />
<TextBlock TextWrapping="Wrap" Height="378" Width="452" Text="{Binding FileText2, Mode=TwoWay}" Name="textBlocky2" />
ReadFilePage:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
AddFileModel model = this.LayoutRoot.DataContext as AddFileModel;
model.ReadSelectedFiles.Execute(null);
}
Below is the Class File in-charge of the method.
AddFileModel Class File:
namespace WindowsPhoneApplication1.Model
{
public class AddFileModel : ModelBase
{
private string _filename;
public string FileName
{
get
{
return this._filename;
}
set
{
this._filename = value;
this.OnPropertyChanged("FileName");
}
}
private string _filetext1;
public string FileText1
{
get
{
return this._filetext1;
}
set
{
this._filetext1 = value;
this.OnPropertyChanged("FileText1");
}
}
private string _filetext2;
public string FileText2
{
get
{
return this._filetext2;
}
set
{
this._filetext2 = value;
this.OnPropertyChanged("FileText2");
}
}
private ICommand _saveFile;
public ICommand SaveFile
{
get
{
this._saveFile = this._saveFile ?? new DelegateCommand(this.OnSaveFile);
return this._saveFile;
}
}
private ICommand _readSelectedFiles;
public ICommand ReadSelectedFiles
{
get
{
this._readSelectedFiles = this._readSelectedFiles ?? new DelegateCommand(this.OnReadSelected);
return this._readSelectedFiles;
}
}
private void OnSaveFile()
{
if (!string.IsNullOrEmpty(this.FileName))
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists(FileName))
store.DeleteFile(FileName);
using (var fileStream = store.OpenFile(FileName, FileMode.Create, FileAccess.Write))
{
using (var writer = new StreamWriter(fileStream))
{
writer.WriteLine(FileName);
writer.WriteLine(FileText1);
writer.WriteLine(FileText2);
}
}
}
}
}
private void OnReadSelected()
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists(FileName))
{
using (var fileStream = store.OpenFile(FileName, FileMode.Open, FileAccess.Read))
{
using (var reader = new StreamReader(fileStream))
{
FileName = reader.ReadLine();
FileText1 = reader.ReadLine();
FileText2 = reader.ReadLine();
}
}
}
else
{
MessageBox.Show("File not found!");
}
}
}
}
}
The code you've posted works fine. Your problem must be elsewhere in your application logic. In your read logic you don't seem to be setting the FileName property before you execute your ReadSelectedFiles command. Your code needs this to be set because this is the file name it tries to look for. Unless you are setting this elsewhere this could be the cause of the problem.
So perhaps something like
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
AddFileModel model = this.LayoutRoot.DataContext as AddFileModel;
model.FileName = "myFile.dat";
model.ReadSelectedFiles.Execute(null);
}
Related
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;
}
}
I have a WPF of tasks each has status of completed or pending, I will view all of these items into a list box, the tasks are serialized into XML file and these items are linked to UI through ObersvableCollection.
What I'm looking for it so be able to filter the tasks on the view and be able to edit or create new taks and save into the XML file.
I was thinking about creating a new class called FilterTask.cs and have static method Completed() which return ObersvableCollection item but I am not sure if this will affect the serialization process.
here is my code
main window XAML
<Window x:Class="UIToDoList_2.MainWindow"
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:UIToDoList_2"
mc:Ignorable="d"
Title="To Do List" Height="290" Width="500" ResizeMode="CanMinimize">
<Grid Margin="0,0,0,0">
<StackPanel Name="MainPanel">
<StackPanel Name="ViewTaskPanel" Margin="10">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Task" Margin="0,0,10,0"/>
<StackPanel Orientation="Vertical">
<ListBox x:Name="UITasks" Width="300" Height="100" VerticalAlignment="Top" Margin="0,0,10,5"
DisplayMemberPath="Name"/>
<RadioButton x:Name="ShowAll" Content="Show All"/>
<RadioButton x:Name="ShowCompleted" Content="Show Completed" />
<RadioButton x:Name="ShowPending" Content="Show Pending"/>
</StackPanel>
<StackPanel>
<Button Content="Mark As Completed" Margin="0,0,0,5"/>
<Button Content="Mark As Pending" Margin="0,0,0,5"/>
<Button x:Name="DltTaskBtn" Content="Delete Task"
Margin="0,0,0,5" Click="DltTaskBtn_Click"/>
<Button x:Name="RenameBtn" Content="Rename" Click="RenameBtn_Click"/>
</StackPanel>
</StackPanel>
</StackPanel>
<StackPanel Name="AddTaskPanel" Margin="10">
<TextBlock Text="Add New Task"/>
<StackPanel Orientation="Horizontal" Margin="2">
<TextBlock Text="Task Name" Margin="0,0,10,0"/>
<TextBox Name="TaskNameTxt" Width="260"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button x:Name="AddTaskBtn" Content="Add" Margin="5" Click="AddTaskBtn_Click"/>
<Button x:Name="ClearTaskTxt" Content="Clear" Margin="5" Click="ClearTaskTxt_Click"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Grid>
</Window>
Task.cs
public class Task : INotifyPropertyChanged
{
private string _Name;
public string Name
{
get { return this._Name; }
set
{
if (this._Name != value)
{
this._Name = value;
this.NotifyPropertyChanged();
}
}
}
private Status _Status;
public Status Status
{
get { return this._Status; }
set
{
if (this._Status != value)
{
this._Status = value;
this.NotifyPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propName = "")
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
public enum Status
{
Pending = 0,
Completed = 1
}
TaskSerialization.cs
public class TaskSerialization
{
public string FileName { get; set; }
public TaskSerialization(string fileName)
{
FileName = fileName;
}
/// <summary>
/// Serialize the task list to XML File
/// </summary>
/// <param name="tasks"></param>
public void SerializeTasks(List<Task> tasks)
{
StreamWriter sw = new StreamWriter(FileName);
XmlSerializer sr = new XmlSerializer(typeof(List<Task>));
sr.Serialize(sw, tasks);
sw.Close();
}
/// <summary>
/// Deserialize the taks list form XML file and return as ObservableCollection item
/// </summary>
/// <returns></returns>
public ObservableCollection<Task> DeserializeTasks()
{
XmlSerializer sr = new XmlSerializer(typeof(List<Task>));
FileStream fs = new FileStream(FileName, FileMode.Open);
return new ObservableCollection<Task>((List<Task>)sr.Deserialize(fs));
}
}
Mainwindow.cs
namespace UIToDoList_2
{
public partial class MainWindow : Window
{
private readonly string fileName = "ToDoList.xml";
private readonly TaskSerialization taskSrlz;
ObservableCollection<Task> tasks = new ObservableCollection<Task>();
public MainWindow()
{
InitializeComponent();
taskSrlz = new TaskSerialization(fileName);
tasks = taskSrlz.DeserializeTasks();
UITasks.ItemsSource = tasks;
}
private void AddTaskBtn_Click(object sender, RoutedEventArgs e)
{
AddTask();
}
private void DltTaskBtn_Click(object sender, RoutedEventArgs e)
{
DeleteTask();
}
private void AddTask()
{
var taskName = TaskNameTxt.Text;
if (!string.IsNullOrWhiteSpace(taskName))
{
tasks.Add(new Task() { Name = TaskNameTxt.Text, Status = Status.Pending });
taskSrlz.SerializeTasks(new List<Task>(tasks));
}
else
MessageBox.Show("Task name cannot be null", "Error in Task name", MessageBoxButton.OK, MessageBoxImage.Error);
}
private void DeleteTask()
{
if(UITasks.SelectedItem != null)
{
var selectedTask = (UITasks.SelectedItem as Task);
tasks.Remove(selectedTask);
taskSrlz.SerializeTasks(new List<Task>(tasks));
}
}
private void RenameBtn_Click(object sender, RoutedEventArgs e)
{
if (UITasks.SelectedItem != null)
{
(UITasks.SelectedItem as Task).Name = TaskNameTxt.Text;
taskSrlz.SerializeTasks(new List<Task>(tasks));
}
}
private void ClearTaskTxt_Click(object sender, RoutedEventArgs e)
{
TaskNameTxt.Clear();
}
}
}
I will mark this as answer based on clemens comment to look into binding into collections https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/data-binding-overview#binding-to-collections
Button in a data template with a command in a list view of items not working. I have tried other data templates with stack panel.
The button to search for the initial items will recognize it's command and work.
Here is the page with the data template for the items:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfFstApp1.Pages"
xmlns:ViewModels="clr-namespace:WpfFstApp1.ViewModels" x:Class="WpfFstApp1.Pages.CsvPage"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="CsvPage">
<Page.DataContext>
<ViewModels:CsvViewModel/>
</Page.DataContext>
<Page.Resources>
<DataTemplate x:Key="CsvTemp">
<ListView>
<TextBlock Text="{Binding Dir}"/>
<TextBlock Text="{Binding FiPath}"/>
<Button Content="Click To Open" Command="{Binding OpenFiCommand, Mode=OneWay}" CommandParameter="{Binding SelResult.FiPath}"></Button>
<ListView ItemsSource="{Binding Refs}"/>
</ListView>
</DataTemplate>
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55"/>
<RowDefinition/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TextBlock Text="Project" HorizontalAlignment="Left" Margin="10,5,0,0" VerticalAlignment="Top" Height="16" Width="37"/>
<TextBlock Text="Sales Line" HorizontalAlignment="Left" Margin="65,5,0,0" VerticalAlignment="Top" Height="16" Width="52"/>
<TextBlock Text="Part Number" HorizontalAlignment="Left" Margin="155,5,0,0" VerticalAlignment="Top" Height="16" Width="68"/>
<TextBox x:Name="TxtBoxProj" HorizontalAlignment="Left" Height="23" Margin="10,26,0,0" VerticalAlignment="Top" Width="50"
TextAlignment="Right" MaxLines="1" Text="{Binding SearchVal1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="TxtBoxSline" HorizontalAlignment="Left" Height="23" Margin="65,26,0,0" VerticalAlignment="Top" Width="85" MaxLines="1" MaxLength="25"
TextAlignment="Right" CharacterCasing="Upper" Text="{Binding SearchVal2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="TxtBoxPart" HorizontalAlignment="Left" Height="23" Margin="155,26,0,0" VerticalAlignment="Top" Width="120"
TextAlignment="Right" MaxLines="1" CharacterCasing="Upper" Text="{Binding SearchVal3, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Button x:Name="BtnSrch" Content="Search" HorizontalAlignment="Left" Margin="280,29,0,0" VerticalAlignment="Top" Width="40" Command="{Binding SearchCommand, Mode=OneWay}"/>
<ListView Margin="10" Grid.Row="1" ItemsSource="{Binding OcResults}" ItemTemplate="{Binding Mode=OneWay, Source={StaticResource CsvTemp}}" SelectedItem="{Binding SelResult}"/>
<StatusBar Grid.Row="2" FontSize="14" Margin="1,0,1,1" VerticalAlignment="Bottom">
<TextBlock x:Name="tsTxtBlck1" Text="{Binding CurStat1}" />
<TextBlock x:Name="tsTxtBlck2" Text="{Binding CurStat2}"/>
</StatusBar>
</Grid>
Here is the Model :
class SearchResult
{
public string Dir { get; set; }
public string FiName { get; set; }
public string FiPath { get; set; }
public List<string> Refs { get; set; }
}
Here is my ViewModel :
class CsvViewModel : ModelBase
{
#region [ Declarations ]
//
bool Init = false;
string CurDir = string.Empty;
//Count of files traversed and timer for diagnostic output
int fileCount = 0;
Stopwatch sw;
public List<SearchResult> ResList;
//
#endregion
#region [ Properties ]
//
public ObservableCollection<SearchResult> OcResults { get; set; }
//
private string _curStat1;
public string CurStat1
{
get { return _curStat1; }
set { _curStat1 = value; OnPropertyChanged(nameof(CurStat1)); }
}
private string _curStat2;
public string CurStat2
{
get { return _curStat2; }
set { _curStat2 = value; OnPropertyChanged(nameof(CurStat2)); }
}
//
private string _searchVal1 = string.Empty;
public string SearchVal1
{
get { return _searchVal1; }
set { _searchVal1 = value; OnPropertyChanged(nameof(SearchVal1)); }
}
private string _searchVal2 = string.Empty;
public string SearchVal2
{
get { return _searchVal2; }
set { _searchVal2 = value; OnPropertyChanged(nameof(SearchVal2)); }
}
private string _searchVal3 = string.Empty;
public string SearchVal3
{
get { return _searchVal3; }
set { _searchVal3 = value; OnPropertyChanged(nameof(SearchVal3)); }
}
//
public RelCom SearchCommand { get; private set; }
public RelCom OpenFiCommand { get; private set; }
//
private SearchResult selResult;
public SearchResult SelResult
{
get { return selResult; }
set { selResult = value; OnPropertyChanged(nameof(SelResult)); }
}
//
#endregion
public CsvViewModel()
{
ResList = new List<SearchResult>();
OcResults = new ObservableCollection<SearchResult>();
SearchCommand = new RelCom(Search_CanExecute, Search);
OpenFiCommand = new RelCom(OpenFi_CanExecute, OpenFile);
}
private bool Search_CanExecute(object obj)
{
return true;
}
private void Search(object obj)
{
CurDir = string.Empty;
//
if (!string.IsNullOrWhiteSpace(SearchVal1))
SearchVal2 = SearchVal1.Trim() + " " + SearchVal2.Trim();
//
if (!string.IsNullOrWhiteSpace(SearchVal3))
SearchVal3 = SearchVal3.Trim();
//
try
{
TraverseTreeParallelForEach(#"\\ast-sigmanest\Feedback\Feedback", (f) =>
{
// Exceptions are no-ops.
try
{
// Do nothing with the data except read it to find string...
FileInfo fi1 = new FileInfo(f);
bool addfi = false;
//
string[] Lines = File.ReadAllLines(f);
List<string> refLines = new List<string>();
foreach (string line in Lines)
{
if (line.Contains(SearchVal2) & line.Contains(SearchVal3))
{
addfi = true;
refLines.Add(line);
}
}
//
if (addfi)
{
SearchResult Sres = new SearchResult { Dir = fi1.Directory.ToString(), FiName = fi1.Name, FiPath = fi1.FullName, Refs = refLines };
ResList.Add(Sres);
}
//
//contents = null;
Lines = null;
fi1 = null;
//
}
catch (FileNotFoundException) { }
catch (IOException) { }
catch (UnauthorizedAccessException) { }
catch (SecurityException) { }
// Display the filename.
//Console.WriteLine(f);
});
}
catch (ArgumentException Ae)
{
MessageBox.Show(Ae.Message);
}
//
var resgrp = (from res in ResList group res by res.Dir into newres orderby newres.Key select newres);
foreach (var rgrp in resgrp)
{
foreach (var res in rgrp)
{
OcResults.Add(res);
}
}
//
}
private bool OpenFi_CanExecute(object obj)
{
return obj != null;
}
private void OpenFile(object obj)
{
Process.Start(new ProcessStartInfo(SelResult.FiPath));
}
public void TraverseTreeParallelForEach(string root, Action<string> action)
{
//Count of files traversed and timer for diagnostic output
fileCount = 0;
sw = Stopwatch.StartNew();
// Determine whether to parallelize file processing on each folder based on processor count.
int procCount = System.Environment.ProcessorCount;
// Data structure to hold names of subfolders to be examined for files.
Stack<string> dirs = new Stack<string>();
if (!Directory.Exists(root))
{
throw new ArgumentException();
}
dirs.Push(root);
while (dirs.Count > 0)
{
string currentDir = dirs.Pop();
string[] subDirs = { };
string[] files = { };
try
{
subDirs = Directory.GetDirectories(currentDir);
// Push the subdirectories onto the stack for traversal.
// This could also be done before handing the files.
foreach (string str in subDirs)
dirs.Push(str);
}
// Thrown if we do not have discovery permission on the directory.
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
continue;
}
// Thrown if another process has deleted the directory after we retrieved its name.
catch (DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
}
//
try
{
files = Directory.GetFiles(currentDir, "*.csv");
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
continue;
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
}
catch (IOException e)
{
Console.WriteLine(e.Message);
continue;
}
//
// Execute in parallel if there are enough files in the directory.
// Otherwise, execute sequentially.Files are opened and processed
// synchronously but this could be modified to perform async I/O.
try
{
if (files.Length < procCount)
{
foreach (var file in files)
{
action(file);
fileCount++;
}
}
else
{
Parallel.ForEach(files, () => 0, (file, loopState, localCount) =>
{
action(file);
return (int)++localCount;
},
(c) =>
{
Interlocked.Add(ref fileCount, c);
});
}
}
catch (AggregateException ae)
{
ae.Handle((ex) =>
{
if (ex is UnauthorizedAccessException)
{
// Here we just output a message and go on.
Console.WriteLine(ex.Message);
return true;
}
// Handle other exceptions here if necessary...
return false;
});
}
//
}
//
}
}
For some reason the button in the data template does not trigger the command, even though it shows to be bound to it.
Any help appreciated.
Thanks.
You need to let the Open command know that the CanExecute conditions have changed, do this in your SelResult setter:
((RelayCommand<object>)OpenFiCommand).RaiseCanExecuteChanged();
This is one of the reasons I don't like using CanExecute.
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.
I'm new to WPF and am working on Databinding a Listbox from a xml file, everything loads correctly when the program starts, however I'm having trouble making the listbox update after I insert a new record. Everything that I've read is pointing to use a ObservableCollection which I am, but I can't figure out how to get the Listbox to refresh. I have tried calling a update to the ItemsSource but it still doesn't seem to work. Ideally I would like to just have a Refresh button that A user can click to update the listbox. Does anyone have any suggestions on a calling a update to the list box
Thanks Michael
public class ContactList
{
string contactFile = #"U:\Peridot\Users\" + Program.getUser.ToString() + ".xml";
public ContactList()
{
}
public ContactList(string contactFullName, string contactCellNumber,string contactBusinessNumber, string contactExtension, string contactEmail, string contactStatus,string contactAuralinkStatus, string contactAuralinkID)
{
this.ContactFullName = contactFullName;
this.ContactCellNumber = contactCellNumber;
this.ContactBusinessNumber = contactBusinessNumber;
this.ContactExtension = contactExtension;
this.ContactEmail = contactEmail;
this.ContactStatus = contactStatus;
this.ContactAuralinkStatus = contactAuralinkStatus;
this.ContactAuralinkID = contactAuralinkID;
}
private string ContactFullName;
public string PropContactFullName
{
get { return ContactFullName; }
set { ContactFullName = value; }
}
private string ContactCellNumber;
public string PropContactCellNumber
{
get { return ContactCellNumber; }
set { ContactCellNumber = value; }
}
private string ContactBusinessNumber;
public string PropContactBusinessNumber
{
get { return ContactBusinessNumber; }
set { ContactBusinessNumber = value; }
}
private string ContactEmail;
public string PropContactEmail
{
get { return ContactEmail; }
set { ContactEmail = value; }
}
private string ContactStatus;
public string PropContactStatus
{
get { return ContactStatus; }
set { ContactStatus = value; }
}
private string ContactAuralinkStatus;
public string PropContactAuralinkStatus
{
get { return ContactAuralinkStatus; }
set { ContactAuralinkStatus = value; }
}
public string ContactAuralinkID;
public string PropContactAuralinkID
{
get { return ContactAuralinkID; }
set { ContactAuralinkID = value; }
}
private string ContactExtension;
public string PropContactExtension
{
get { return ContactExtension; }
set { ContactExtension = value; }
}
}
public class Contacts : System.Collections.ObjectModel.ObservableCollection<ContactList>
{
string contactFile = #"U:\Peridot\Users\" + Program.getUser.ToString() + ".xml";
//Added this
public event NotifyCollectionChangedEventHandler CollectionChanged;
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
{
CollectionChanged(this, e);
}
}
public Contacts(): base()
{
getContactFile();
XDocument doc = XDocument.Load(contactFile);
var contacts = from r in doc.Descendants("Contact")
select new
{
FullName = r.Element("FullName").Value,
CellNumber = r.Element("CellNumber").Value,
BusinessNumber = r.Element("BusinessNumber").Value,
Extension = r.Element("Extension").Value,
Email = r.Element("Email").Value,
AuralinkID = r.Element("AuralinkID").Value
};
foreach (var r in contacts)
{
Add(new ContactList(r.FullName,r.CellNumber , r.BusinessNumber,r.Extension, r.Email, "", "",r.AuralinkID));
}
}
private void getContactFile()
{
if (!File.Exists(contactFile))
{
new XDocument(
new XElement("Contacts"
)
)
.Save(contactFile);
}
}
}
private void addContactICON_MouseDown(object sender, MouseButtonEventArgs e)
{
if (!doesContactExist())
{
try
{
XDocument doc = XDocument.Load(#"U:\Peridot\Users\" + Program.getUser.ToString() + ".xml");
XElement contact = new XElement("Contact");
contact.Add(new XElement("ContactID", contactID.ToString()));
contact.Add(new XElement("FullName", contactNameLBL.Content.ToString()));
contact.Add(new XElement("CellNumber", c1.Content.ToString()));
contact.Add(new XElement("BusinessNumber", businessPhoneIcon.ToolTip.ToString()));
contact.Add(new XElement("Extension", c3.Content.ToString()));
contact.Add(new XElement("Email", emailIcon.ToolTip.ToString()));
contact.Add(new XElement("AuralinkID", videoIcon.ToolTip.ToString()));
doc.Element("Contacts").Add(contact);
doc.Save(#"U:\Peridot\Users\" + Program.getUser.ToString() + ".xml");
MessageBox.Show(contactNameLBL.Content.ToString() + " has been added to your contacts.");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
MessageBox.Show("Contact Already Exists");
}
XAML
<StackPanel>
<StackPanel.Resources>
<local:Contacts x:Key="contactListobj"></local:Contacts>
</StackPanel.Resources>
<ListBox x:Name="contactList" Width="305" Margin="5,3,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource contactListobj}}" Height="450" IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" >
<TextBlock Text="{Binding PropContactFullName}" ToolTip="{Binding PropContactFullName}" Height="35" Width="175" FontSize="12"/>
<TextBlock x:Name="contactEmailLBL" Text="{Binding PropContactEmail}" ToolTip="{Binding PropContactEmail}" Cursor="Hand" Width="30" Height="35" MouseLeftButtonUp="contactEmailLBL_MouseLeftButtonUp" Foreground="{x:Null}" FontSize="1">
<TextBlock.Background>
<ImageBrush Stretch="Uniform" ImageSource="Images/emailICON.png"/>
</TextBlock.Background>
</TextBlock>
<TextBlock x:Name="cellNumberLBL" Text="{Binding PropContactCellNumber}" ToolTip="{Binding PropContactCellNumber}" Cursor="Hand" MouseLeftButtonUp="cellNumberLBL_MouseLeftButtonUp" Width="30" Height="35" Foreground="{x:Null}" FontSize="1">
<TextBlock.Background>
<ImageBrush Stretch="Uniform" ImageSource="Images/mobilePhoneICON.png"/>
</TextBlock.Background>
</TextBlock>
<TextBlock x:Name="businessNumberLBL" Text="{Binding PropContactBusinessNumber}" ToolTip="{Binding PropContactBusinessNumber}" Cursor="Hand" Width="30" Height="35" MouseLeftButtonUp="businessNumberLBL_MouseLeftButtonUp" Foreground="{x:Null}" FontSize="1">
<TextBlock.Background>
<ImageBrush Stretch="Uniform" ImageSource="Images/BusinessPhoneICON.png"/>
</TextBlock.Background>
</TextBlock>
<TextBlock x:Name="auralinkLBL" Text="{Binding PropContactAuralinkID}" ToolTip="{Binding PropContactAuralinkID}" Cursor="Hand" Width="30" Height="35" Foreground="{x:Null}" FontSize="1" MouseLeftButtonUp="auralinkLBL_MouseLeftButtonUp">
<TextBlock.Background>
<ImageBrush Stretch="Uniform" ImageSource="Images/VideoICON.png"/>
</TextBlock.Background>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
From what I can tell based on the source code for ObservableCollection, the problem is most likely that the Add method you are using to add ContactList objects to your ObservableCollection is part of the Collection class that ObservableCollection inherits from. This does not fire the CollectionChanged event on the ObservableCollection so your binding is never notified that the collection has changed. Try calling the OnCollectionChanged protected method after you add each item to the collection.