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.
Related
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 am using json webservice after getting the responce binding to listview offset based.starting only 20 records binding after that scroll the listview again remaning records binding through offset based last id passed to service?how can i implement this feature...?
This is my code:
<ListView x:Name="GetallquestionListview" ItemsSource="{Binding}" Background="White" BorderThickness="0" Margin="0" Height="Auto" VerticalAlignment="Stretch" ItemContainerStyle="{StaticResource Latestitemcontainerstyle}"
ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Padding="0" VerticalContentAlignment="Stretch" SelectionChanged="GetallquestionListview_SelectionChanged" >
<ListView.Header>
<ProgressBar x:Name="myIndeterminateProbar" IsIndeterminate="True" Height="50" Visibility="Collapsed"></ProgressBar>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<Border BorderThickness="0,0,0,1" HorizontalAlignment="Stretch" RequestedTheme="Light" BorderBrush="#aaBFBCBC" >
<StackPanel Orientation="Vertical" Width="Auto" >
<TextBlock Margin="0" x:Name="Name" Text="{Binding question_text}" TextWrapping="Wrap" Foreground="#FF110202" FontSize="18" FontFamily="Segoe UI Semibold" Padding="8,15,8,3" SelectionHighlightColor="White" ></TextBlock>
<Image Margin="8" x:Name="image1" Source="{Binding thumbnail_path}" MaxHeight="200" Stretch="UniformToFill"></Image>
<TextBlock Margin="0" x:Name="FristName" Text="{Binding total_votes}" TextWrapping="Wrap" Foreground="#FF03A9F5" FontSize="16" FontFamily="Segoe WP Semibold" Padding="8,3,8,10" FontWeight="Bold" ></TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This my code :
private async void Getallquestion()
{
try
{
myIndeterminateProbar.Visibility = Visibility.Visible;
if (App.IsInternetAvailable)
{
string Longitude = await Locations.GetLongitude();
string Lattitude = await Locations.GetLattitude();
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
string Sortigntype;
// Create a simple setting
Object value = localSettings.Values["Login_Data"];
string UserId = value.ToString();
//Calling Webservice
string QuestionsList = await WebServices.GetAllQuestionByoffset(Convert.ToInt32(UserId), 20, Lastoffsetindex, LastquestionId, Convert.ToDouble(Lattitude), Convert.ToDouble(Longitude), lastsyncime, 1);
if (QuestionsList != null)
{
DataContractJsonSerializer obj1 = new DataContractJsonSerializer(typeof(UserMessage));
UserMessage Questionstatusobject = obj1.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(QuestionsList))) as UserMessage;
string StatusId = Questionstatusobject.status_id.ToString();
string Message = Questionstatusobject.message;
if (StatusId == "1")
{
JObject retun_json = JObject.Parse(QuestionsList);
JToken token = JToken.Parse(QuestionsList);
JArray questionss = (JArray)token.SelectToken("questions");
string votetext;
for (int i = 0; i < questionss.Count; ++i)
{
Questions ques = new Questions();
// string questiontext;
//if (questionss[i]["question_text"].ToString() == "")
//{
// ques.question_text = questionss[i]["question_text"].ToString();
//}
//else
//{
// questiontext = Locations.EncodeDecodeBase64(questionss[i]["question_text"].ToString());
//}
// ques.question_text = Locations.EncodeDecodeBase64(ques.question_text);
ques.question_text = Locations.DecodeFrom64(questionss[i]["question_text"].ToString());
// ques.question_text = questionss[i]["question_text"].ToString();
ques.thumbnail_path = questionss[i]["thumbnail_path"].ToString();
ques.question_id = Convert.ToInt32(questionss[i]["question_id"]);
ques.user_id = Convert.ToInt32(questionss[i]["user_id"]);
if (questionss[i]["total_votes"].ToString() == "1")
{
votetext = "Vote";
}
else
{
votetext = "Votes";
}
ques.total_votes = questionss[i]["total_votes"] + " " + votetext;
GetallquestionListview.Items.Add(ques);
GetAllquestionsdata.Add(ques);
}
}
else
{
MessageDialog msgbox2 = new MessageDialog("" + Message + "");
await msgbox2.ShowAsync();
}
}
else
{
MessageDialog msgbox = new MessageDialog("Oops! something went wrong. Please try again.");
await msgbox.ShowAsync();
}
}
else
{
MessageDialog msgbox = new MessageDialog("Please check your Internet connection.");
await msgbox.ShowAsync();
}
}
catch(Exception ex)
{
}
myIndeterminateProbar.Visibility = Visibility.Collapsed;
}
I think you have to check for ISupportIncrementalLoading interface. check out this
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.data.isupportincrementalloading%28v=win.10%29.aspx
This is an example http://www.codeproject.com/Articles/228873/Loading-Data-when-the-User-Scrolls-to-the-End-of
You need to implement the ISupportIncrementalLoading interface in your collection.
The MSDN provides an example implementation.
public class BaseIncrementalObservableCollection<T> : ObservableCollection<T>
#region ISupportIncrementalLoading
public bool HasMoreItems
{
get { return HasMoreItemsOverride(); }
}
public Windows.Foundation.IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
if (_busy)
{
throw new InvalidOperationException("Only one operation in flight at a time");
}
_busy = true;
return AsyncInfo.Run((c) => LoadMoreItemsAsync(c, count));
}
#endregion
#region INotifyCollectionChanged
public event NotifyCollectionChangedEventHandler CollectionChanged;
#endregion
#region Private methods
async Task<LoadMoreItemsResult> LoadMoreItemsAsync(CancellationToken c, uint count)
{
try
{
var items = await LoadMoreItemsOverrideAsync(c, count);
var baseIndex = _storage.Count;
_storage.AddRange(items);
// Now notify of the new items
NotifyOfInsertedItems(baseIndex, items.Count);
return new LoadMoreItemsResult { Count = (uint)items.Count };
}
finally
{
_busy = false;
}
}
void NotifyOfInsertedItems(int baseIndex, int count)
{
if (CollectionChanged == null)
{
return;
}
for (int i = 0; i < count; i++)
{
var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, _storage[i + baseIndex], i + baseIndex);
CollectionChanged(this, args);
}
}
#endregion
#region Overridable methods
protected abstract Task<IList<T>> LoadMoreItemsOverrideAsync(CancellationToken c, uint count);
protected abstract bool HasMoreItemsOverride();
#endregion
#region State
List<T> _storage = new List<T>();
bool _busy = false;
#endregion
}
Then you can derive from this and implement the concrete requests.
public class UserIncrementalObservableCollection : BaseIncrementalObservableCollection<User>
{
protected override Task<IList<User>> LoadMoreItemsOverrideAsync(CancellationToken c, uint count)
{
// your call to the webservice
}
protected override bool HasMoreItemsOverride()
{
// check if there are more
}
}
Alternatively, pass Func<CancellationToken, uint, IList<T>> and Func<bool> delegates into your class, so you can avoid implementing dozens of concrete classes if there is a big number of incremental lists.
I am developing an windows phone applicaiton using sqlite database.I have stored all the values in history.xaml.cs but when I run the applicaiton it ends being error on debugger.break as "> SmartParking.DLL!SmartParking.App.RootFrame_NavigationFailed(object sender, System.Windows.Navigation.NavigationFailedEventArgs e) Line 133 C#"
Below are all my 3 classes that I am dealing with to store the values in the database and output it in the history.xaml
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="Smart Parking" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="History" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="ListData">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name= "DateTxt" Text="{Binding Date}" TextWrapping="Wrap" />
<TextBlock x:Name= "TimeTxt" Text="{Binding Time}" TextWrapping="Wrap" />
<TextBlock x:Name= "ZoneTxt" Text="{Binding Zone}" TextWrapping="Wrap"/>
<TextBlock x:Name= "FloorTxt" Text="{Binding Floor}" TextWrapping="Wrap"/>
<TextBlock x:Name= "LatTxt" Text="{Binding location_latitude}" TextWrapping="Wrap" />
<TextBlock x:Name= "LongTxt" Text="{Binding location_longitude}" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
below is dbhelper that carries out all the main functions
public class DbHelper
{
SQLiteConnection dbConn;
public async Task<bool> onCreate(string DB_PATH)
{
try
{
if (!CheckFileExists(DB_PATH).Result)
{
using (dbConn = new SQLiteConnection(DB_PATH))
{
dbConn.CreateTable<historyTableSQlite>();
}
}
return true;
}
catch
{
return false;
}
}
private async Task<bool> CheckFileExists(string fileName)
{
try
{
var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
return true;
}
catch
{
return false;
}
}
//retrieve all list from the database
public ObservableCollection<historyTableSQlite> ReadHistory()
{
using (var dbConn = new SQLiteConnection(App.DB_PATH))
{
List<historyTableSQlite> myCollection = dbConn.Table<historyTableSQlite>().ToList<historyTableSQlite>();
ObservableCollection<historyTableSQlite> HistoryList = new ObservableCollection<historyTableSQlite>(myCollection);
return HistoryList;
}
}
// Insert the new info in the histrorytablesqlite table.
public void Insert(historyTableSQlite newcontact)
{
using (var dbConn = new SQLiteConnection(App.DB_PATH))
{
dbConn.RunInTransaction(() =>
{
dbConn.Insert(newcontact);
});
}
}
}
here is the history.xaml.cs code file
public partial class History : PhoneApplicationPage
{
// string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
ObservableCollection<historyTableSQlite> DB_HistoryList = new ObservableCollection<historyTableSQlite>();
public History()
{
InitializeComponent();
AddInfo();
ReadHistoryList_Loaded();
}
public void ReadHistoryList_Loaded()
{
ReadAllContactsList dbhistory = new ReadAllContactsList();
DB_HistoryList = dbhistory.GetAllHistory();//Get all DB contacts
ListData.ItemsSource = DB_HistoryList.OrderByDescending(i => i.Id).ToList();//Latest contact ID can Display first
}
public void AddInfo()
{
// ListData.Items.Add(new historyTableSQlite{
// Date = DateTime.Now.ToShortDateString(),
// Time = DateTime.Now.ToShortTimeString(),
// Zone = "PST",
// Floor = "10th Floor",
// latitude = 35.45112,
// longtitude = -115.42622
//});
DbHelper Db_helper = new DbHelper();
Db_helper.Insert((new historyTableSQlite {
Date = DateTime.Now.ToShortDateString(),
Time = DateTime.Now.ToShortTimeString(),
Zone = "PST",
Floor = "10th Floor",
latitude = 35.45112,
longtitude = -115.42622
}));
}
}
and the last is the class where I intialize all my values
ublic class historyTableSQlite : INotifyPropertyChanged
{
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int Id { get; set; }
private int idvalue;
private string dateValue = string.Empty;
public string Date {
get { return this.dateValue; }
set
{
if (value != this.dateValue)
{
this.dateValue = value;
NotifyPropertyChanged("Date");
}
}
}
private string timeValue = string.Empty;
public string Time
{
get { return this.timeValue; }
set
{
if (value != this.timeValue)
{
this.timeValue = value;
NotifyPropertyChanged("Time");
}
}
}
private string floorValue = string.Empty;
public string Floor
{
get { return this.floorValue; }
set
{
if (value != this.floorValue)
{
this.floorValue = value;
NotifyPropertyChanged("Floor");
}
}
}
public string zoneValue;
public string Zone
{
get { return this.zoneValue; }
set
{
if (value != this.zoneValue)
{
this.zoneValue = value;
NotifyPropertyChanged("Zone");
}
}
}
private double latValue;
public double latitude
{
get { return latValue; }
set
{
if (value != this.latValue)
{
this.latValue = value;
NotifyPropertyChanged("Latitude");
}
}
}
private double lonValue;
public double longtitude
{
get { return this.lonValue; }
set
{
if (value != this.lonValue)
{
this.lonValue = value;
NotifyPropertyChanged("Longitude");
}
}
}
// public string isMarkPoint { get; set; }
public historyTableSQlite()
{
}
public historyTableSQlite(string date,string time,string floor,string zone,double lat,double lng)
{
Date = date;
Time = time;
Floor = floor;
Zone = zone;
latitude = lat;
longtitude = lng;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
Remove AddInfo() and ReadHistoryList_Loaded() from the History constructor and override OnNavigatedTo() like this.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
AddInfo();
ReadHistoryList_Loaded();
}
If it the page constructor takes long than a few hundred millisecond to create the page windows will assume something has gone wrong. It is best not to put anything in the constructor for this reason.
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.
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);
}