WPF Tookit - IntegerUpDown - c#

How do I get the value from the IntergerUpDown control?
I'am using this : http://wpftoolkit.codeplex.com/wikipage?title=NumericUpDown
Here is the code from MainWindow.xaml
<extToolkit:IntegerUpDown Value="{Binding Mode=TwoWay, Path=CurrentCount}" HorizontalAlignment="Left" Margin="119,111,0,148" Increment="1" Maximum="10" Minimum="1" Width="100"/>
Here is the code from MainWindow.xaml.cs
public int CurrentCount { get; set; }
private void button1_Click(object sender, RoutedEventArgs e)
{
CurrentCount = 10;
int len = Talker.BlahBlahBlah(textBox1.Text, CurrentCount);
}
So basically I want to pass the value chosen by the user as an int into the method BlahBlahBlah. Do I need to create a View Model and bind it to a CurrentValue property? Can you please provide me with sample code which gets the value from the UI?
So here is my Talker Class:
class Talker
{
public static int BlahBlahBlah(string thingToSay, int numberOfTimes)
{
string finalString = "";
for (int i = 0; i < numberOfTimes; i++)
{
finalString = finalString + thingToSay + "\n";
}
MessageBox.Show(finalString);
return finalString.Length;
}
}
This is the method inside ViewModelBase:
public virtual int CurrentCount
{
get { return _CurrentCount; }
set
{
_CurrentCount = value;
OnPropertyChanged("CurrentCount");
}
}
Question is how do I link it together??
Peace
Andrew

The proper, MVVM-correct way of doing this is to bind the Value to a property on your ViewModel.
public int CurrentCount
{
get { return _CurrentCount; }
set
{
_CurrentCount = value;
}
}
I recommend Josh Smith's excellent article on MVVM.
One thing you might want to do later is update the CurrentCount from code and have it reflect correctly in the IntegerUpDown control. To do this, you'll have to inherit from the INotifyPropertyChanged interface. You can use his ViewModelBase class which does just that. Then, your ViewModel inherits from ViewModelBase and can call OnPropertyChanged to send property changed notifications:
public int CurrentCount
{
get { return _CurrentCount; }
set
{
_CurrentCount = value;
base.OnPropertyChanged("CurrentCount");
}
}

One way to do it would be to name the IntegerUpDown control
<extToolkit:IntegerUpDown
x:Name="CurrentCountUpDown"
HorizontalAlignment="Left"
Margin="119,111,0,148"
Increment="1"
Maximum="10"
Minimum="1"
Width="100"/>
And then just refer to that control by name in your event handler:
private void button1_Click(object sender, RoutedEventArgs e)
{
int len = Talker.BlahBlahBlah(textBox1.Text, CurrentCountUpDown.Value);
}

Related

WPF DataGrid, ObservableCollection Updates, and Redrawing

I will try to keep this as brief as possible, but there is a fair amount of nuance to this question.
The Workflow
I am working in C# and am using WPF and MVVM for the UI for an addin for Revit (a 3D modeling software from Autodesk).
The overarching goal is to create a window that shows the parameters of a 3D element after it is selected. This is a filtered list that are specific to my organization and our users needs, and allow the user to edit them in order to streamline their workflow. The complication is that because I am working with an API I can only use what tools I am given when interacting with the model.
The issue I am running into lies in the workflow. I have detailed the workflow below.
User Selects a 3D Element
The addin uses the API to pull the parameters and wraps them in a wrapper class and adds them into a custom ObservableCollection to display them in the DataGrid
The user then changes a value in the DataGrid. When the cell loses focus it fires off a command that hooks the API and updates the parameter's value.
The change is made and the internal logic of the element calculates it's values based on the changed parameter
The calculated parameter values are changed in the model
The ViewModel checks each parameter to see if its value has changed, and updates any of the wrapped parameters in the ObservableCollection to reflect the changes.
The ObservableCollection fires off it's collection changed event to notify the DataGrid that values have changed
The DataGrid updates it's values to complete the process.
The issue currently lies in the very last step. Once the collection changed event is complete the wrapped parameter value matches the parameter value from the API, but the DataGrid will not redraw the information. Once you minimize the window, click into the cell, or scroll the DataGrid to where the cell is not visible the cell will show the new value when it comes back into view.
I can't seem to find a way to keep with MVVM principles and force the cells to redraw with their updated value. Am I missing something with this? How do I get the DataGrid to update without having to completely clear and reset the ObservableCollection items?
Things I have Tried
I had to create a custom ObservableCollection to implement INPC for the items in the collection, and from debugging it appears to work as intended. Each time an item in the ObservableCollection is updated it makes the change subscribes it to INPC and raises the collection changed event.
For each of the columns I have the binding set to Mode="TwoWay" and have tried setting the UpdateSourceTrigger="PropertyChanged", and neither helped.
I originally was using a <ContentPresenter/> in a <DataGridTemplteColumn/> to present different cell types, but even using a basic <DataGridTextColumn doesn't work.
---- CODE ----
XAML:
<DataGrid Grid.Row="2" ItemsSource="{Binding TestingParameters, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Name"/>
<DataGridTextColumn IsReadOnly="False" Binding="{Binding Path=ParamValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Param Value">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus">
<i:InvokeCommandAction Command="{Binding UpdateParametersCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
C# ViewModel
public class ViewModelParameterPane : ViewModelBase
{
private ExternalEvent _event;
private HandlerED _handler;
private UIApplication _uiapp;
private UIDocument _uidoc;
private Document _doc;
private TestObservableCollection<WrappedParameter> _testParameter = new TestObservableCollection<WrappedParameter>();
public TestObservableCollection<WrappedParameter> TestParameter
{
get => _testParameter;
set
{
_testParameter = value;
RaiseProperty(nameof(_testParameter));
}
}
public ViewModelParameterPane(ExternalEvent exEvent, HandlerED handler, UIApplication uiapp)
{
_event = exEvent;
_handler = handler;
_uiapp = uiapp;
_uidoc = _uiapp.ActiveUIDocument;
_doc = _uidoc.Document;
_testParameter.ItemPropertyChanged += _testParameter_ItemPropertyChanged;
_testParameter.CollectionChanged += _testParameter_CollectionChanged;
UpdateParametersCommand = new RelayCommand(CallUpdateParameters);
}
private void _testParameter_ItemPropertyChanged(object sender, ItemPropertyChangedEventArgs<WrappedParameter> e)
{
Debug.WriteLine("PROPERTY CHANGE");
RaiseProperty(nameof(TestParameter));
int index = TestParameter.IndexOf(e.Item);
TestParameter[index] = e.Item;
}
private void _testParameter_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Debug.WriteLine("COLLECTION CHANGE");
}
private void MakeRequest(RequestIdED request)
{
_handler.Request.Make(request);
_event.Raise();
}
private void CallUpdateParameters() { MakeRequest(RequestIdED.UpdateParameters); }
public void UpdateParameters()
{
Debug.WriteLine("Running Update Parameters");
try
{
using (var transaction = new Transaction(_doc))
{
transaction.Start("T_UpdateParameters");
foreach (WrappedParameter p in TestParameter)
{
string currenValue = p.RevitParameter.AsValueString();
if (p.RevitParameter.AsValueString() != p.ParamValue)
{
bool setValueSuccess = p.SetRevitParameterValue(p.ParamValue);
if(!setValueSuccess)
{
TaskDialog.Show("Parameter Value Not Set", "The parameter value for the parameter " + p.Name + " was not given a valid value and was not changed. Please ensure the units are correct.");
}
}
}
transaction.Commit();
}
}
catch (Exception e)
{
throw new Exception("Something Went Wrong. Check your values.");
}
}
public void UpdateParameterValues()
{
for(var i = 0; i < TestParameter.Count; i++)
{
TestParameter[i].UpdateValues();
}
}
}
C# Parameter Wrapper Class
public class TestParameter : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
internal void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string _name;
public string Name
{
get => _name;
set
{
_name = value;
OnPropertyChanged(nameof(_name));
}
}
private string _categories;
public string Categories
{
get => _categories;
set
{
_categories = value;
OnPropertyChanged(nameof(_categories));
}
}
private string _paramValue;
public string ParamValue
{
get => _paramValue;
set
{
_paramValue = value;
OnPropertyChanged(nameof(_paramValue));
}
}
private Parameter _revitParameter;
public Parameter RevitParameter
{
get => _revitParameter;
set
{
_revitParameter = value;
OnPropertyChanged(nameof(_revitParameter));
}
}
private ElementId _elementId;
public ElementId ElementId
{
get => _elementId;
set
{
_elementId = value;
OnPropertyChanged(nameof(_elementId));
}
}
public TestParameter(Parameter param)
{
GetRevitParameterValue();
}
public void GetRevitParameterValue()
{
//Get parameter value logic
}
public bool SetRevitParameterValue(string Value)
{
//Set parameter value logic
}
}
C# TestObservableCollection Class
public class TestObservableCollection<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
public event EventHandler<ItemPropertyChangedEventArgs<T>> ItemPropertyChanged;
protected override void InsertItem(int index, T item)
{
base.InsertItem(index, item);
item.PropertyChanged += item_PropertyChanged;
}
protected override void RemoveItem(int index)
{
var item = this[index];
base.RemoveItem(index);
item.PropertyChanged -= item_PropertyChanged;
}
protected override void ClearItems()
{
foreach (var item in this)
{
item.PropertyChanged -= item_PropertyChanged;
}
base.ClearItems();
}
protected override void SetItem(int index, T item)
{
var oldItem = this[index];
oldItem.PropertyChanged -= item_PropertyChanged;
base.SetItem(index, item);
item.PropertyChanged += item_PropertyChanged;
}
private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
OnItemPropertyChanged((T)sender, e.PropertyName);
}
private void OnItemPropertyChanged(T item, string propertyName)
{
var handler = this.ItemPropertyChanged;
if (handler != null)
{
handler(this, new ItemPropertyChangedEventArgs<T>(item, propertyName));
}
}
}
public sealed class ItemPropertyChangedEventArgs<T> : EventArgs
{
private readonly T _item;
private readonly string _propertyName;
public ItemPropertyChangedEventArgs(T item, string propertyName)
{
_item = item;
_propertyName = propertyName;
}
public T Item
{
get { return _item; }
}
public string PropertyName
{
get { return _propertyName; }
}
}

My selectionchanged event is not updating the view, What am I missing?

All fields are marked as TwoWay on databinding, but its obvious I have something wrong. What I have is a page showing a view to add new Devices on one side of the view, and a list of Devices on the other side.. What I'm trying to do is when selecting an item from listview, it will update values within the TextBox for viewing and editing purposes.
The Save option (not shown in Code Below) currently works when I create a new Device, and will refresh the list. however, right now I'm Going back a Frame when complete. What I would like to do is refresh ListView when I click save.
Values from XAML page
<TextBox PlaceholderText="Host Name" Text="{x:Bind ViewModel.HostName, Mode=TwoWay}" Name="hostNameTB" AcceptsReturn="True" />
<TextBox PlaceholderText="Drive Model" Text="{x:Bind ViewModel.DriveModel, Mode=TwoWay}" Name="driveModelTB" />
<TextBox PlaceholderText="Drive SN" Text="{x:Bind ViewModel.DriveSN, Mode=TwoWay}" Name="driveSNTB" AcceptsReturn="True" InputScope="Digits"/>
Code from ViewModel
private Device _ActiveDevice;
private int _HostName;
//All Variables Created for DataBinding to XAML page
//public int HostName { get { return _HostName; } set { _ActiveDevice.HostName = value; } }
public int HostName { get; set; }
public string DriveModel { get; set; }
public string DriveSN { get; set; }
public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState)
{
Value = (suspensionState.ContainsKey(nameof(Value))) ? suspensionState[nameof(Value)]?.ToString() : parameter?.ToString();
await Task.CompletedTask;
var uri = new Uri("http://localhost:2463/api/Devices");
HttpClient client = new HttpClient();
try
{
var JsonResponse = await client.GetStringAsync(uri);
var devicesResult = JsonConvert.DeserializeObject<List<Device>>(JsonResponse);
Devices = devicesResult;
_ActiveDevice = JsonConvert.DeserializeObject<List<Device>>(JsonResponse)[0];
}
catch
{
MessageDialog dialog = new MessageDialog("Unable to Access WebService at this Time!");
await dialog.ShowAsync();
}
//client.Dispose();
}
public void deviceList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var device = ((sender as ListView).SelectedItem as Device);
_ActiveDevice = device;
HostName = device.HostName;
DriveModel = device.DriveModel;
DriveSN = device.DriveSN;
}
You have to inherit the view model from INotifyPropertyChanged to let the binding know there was an update of the value.
public class MainViewModel: INotifyPropertyChanged
{
public int HostName { get => hostName; set { hostName = value; OnPropertyChanged("HostName"); } }
private int hostName;
...
void OnPropertyChanged(String prop)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}
public event PropertyChangedEventHandler PropertyChanged;
}
Update your binding (and all the others) to this:
<TextBox PlaceholderText="Host Name" Text="{Binding ViewModel.HostName, UpdateSourceTrigger=PropertyChanged}" Name="hostNameTB" AcceptsReturn="True" />
I was way off.. my fix was more of what Sir Rufo proposed.. The XAML was correct, but I needed to set the Get and Set of the property to update the property, and then to make sure selected device was update each property.
private int _HostName;
public int HostName { get { return _HostName; } set { Set(ref _HostName, value); } }
public void deviceList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var device = ((sender as ListView).SelectedItem as Device);
//_ActiveDevice = device;
HostName = device.HostName;
DriveModel = device.DriveModel;
DriveSN = device.DriveSN;

WPF percentage status shown in label MVVM

I got some problem in showing download percentage in GridView of WCF. I used MVVM pattern.
Here is my background worker in application start:
public partial class MainWindow : Window
{
public MainWindow()
{
Overall.EverythingOk = "Nothing";
InitializeComponent();
//IRepo repo = new Repo();
ViewModel.MainWindowsViewModel viewModel = new ViewModel.MainWindowsViewModel();
this.DataContext = viewModel;
BackGroundThread bgT = new BackGroundThread();
bgT.bgWrk.RunWorkerAsync();
}}
Here is the DoWork function in BackGroundTHread class
public void bw_DoWork(object sender, DoWorkEventArgs e)
{
if (!Overall.stopStatus)
{
for (int i=0; i < 10000; i++)
{
Overall.PercentageDwnd = i;
Overall.caseRefId = "999999";
if (i == 9998)
{
i = 1;
}
}
}
}
Overall.PercentageDwnd and Overall.caseRefId are static variable (you can call from everywhere in the application) and always update until the background worker completed. I got another ViewModel called TestViewModel and here it is.
public class TestViewModel:BindableBase
{
private String _UpdatePer=Overall.PercentageDwnd.ToString();
public String UpdatePercentage
{
get { return _UpdatePer; }
set { SetProperty(ref _UpdatePer, value); }
}
private ObservableCollection _ViewAKA = new ObservableCollection();
private tblTransaction model;
public TestViewModel(tblTransaction model)
{
// TODO: Complete member initialization
}
public ObservableCollection ViewAKA
{
get { return _ViewAKA; }
set { SetProperty(ref _ViewAKA, value); }
}
}
I bind with TestView.xaml file
<Window x:Class="EmployeeManager.View.TestView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestView" Height="359.774" Width="542.481">
<Grid Margin="0,0,2,0">
<Label Content="{Binding UpdatePercentage,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Background="Red" Foreground="White" Margin="130,86,0,0" VerticalAlignment="Top" Width="132" Height="39">
</Label>
</Grid>
</Window>
There is no real time update at Label even though I bind UpdatePercentage to it. How can I update real time to label?
The problem is that you are updating the static properties, which are not bound to anything. You need to update and raise the property changed notification for the properties which are bound to the label controls, i.e. UpdatePercentage
Can you pass the TestViewModel instance into the RunWorkerAsync call?
bgT.bgWrk.RunWorkerAsync(testViewModel);
And then access in the DoWork event handler:
public void bw_DoWork(object sender, DoWorkEventArgs e)
{
if (!Overall.stopStatus)
{
var viewModel = e.Argument as TestViewModel;
for (int i=0; i < 10000; i++)
{
Overall.PercentageDwnd = i;
viewModel.UpdatePercentage = i;
Overall.caseRefId = "999999";
if (i == 9998)
{
i = 1;
}
}
}
}
Here is answer link:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/02a7b9d1-1c26-4aee-a137-5455fee175b9/wpf-percentage-status-shown-in-label-mvvm?forum=wpf
i need to trigger when the Overall.PercentageDwnd property changes.
Edited
In Overall Class:
public class Overall
{
private static int _percentage;
public static int PercentageDwnd
{
get { return _percentage; }
set
{
_percentage = value;
//raise event:
if (PercentageDwndChanged != null)
PercentageDwndChanged(null, EventArgs.Empty);
}
}
public static string caseRefId { get; set; }
public static bool stopStatus { get; set; }
public static event EventHandler PercentageDwndChanged;
}
In TestViewModel:
public class TestViewModel : BindableBase
{
private String _UpdatePer = Overall.PercentageDwnd.ToString();
public String UpdatePercentage
{
get { return _UpdatePer; }
set { SetProperty(ref _UpdatePer, value); }
}
public TestViewModel(tblTransaction model)
{
Overall.PercentageDwndChanged += Overall_PercentageDwndChanged;
// TODO: Complete member initialization
}
private void Overall_PercentageDwndChanged(object sender, EventArgs e)
{
this.UpdatePercentage = Overall.PercentageDwnd.ToString();
}
}
Since you have bound the TextBlock in the view to the UpdatePercentage source property, you need to set this one and raise the PropertyChanged event whenever you want to update the Label in the view. This means that you need to know when the Overall.PercentageDwnd property changes.
Credit to
Magnus (MM8)
(MCC, Partner, MVP)
Thanks All

How to Pass a Value From a Window to a UserControl in WPF

I want to pass a value from MainWindow into my UserControl! I passed a value to my UserControl and the UserControl showed me the value in a MessageBox, but it is not showing the value in a TextBox. Here is my code:
MainWindow(Passing Value To UserControl)
try
{
GroupsItems abc = null;
if (abc == null)
{
abc = new GroupsItems();
abc.MyParent = this;
abc.passedv(e.ToString(), this);
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
UserControl
public partial class GroupsItems : UserControl
{
public MainWindow MyParent { get; set; }
string idd = "";
public GroupsItems()
{
InitializeComponent();
data();
}
public void passedv(string id, MainWindow mp)
{
idd = id.ToString();
MessageBox.Show(idd);
data();
}
public void data()
{
if (idd!="")
{
MessageBox.Show(idd);
texbox.Text = idd;
}
}
}
EDIT(using BINDING and INotifyProperty )
.....
public GroupsItems()
{
InitializeComponent();
}
public void passedv()
{
textbox1.Text = Text;
}
}
public class Groupitm : INotifyPropertyChanged
{
private string _text = "";
public string Text
{
get { return _text; }
set
{
if (value != _text)
{
_text = value;
NotifyPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Problem here is with reference.
When you create new object in code behind, new object will be created and this is not the same object which you have in xaml code. So you should use following code:
<local:GroupsItems x:Name="myGroupsItems"/>
and in code behind you don't have to create new object. You should use object that you added in XAML:
...
myGroupsItems.MyParent = this;
myGroupsItems.passedv(e.ToString(), this);
...
Here is example solution (sampleproject).
You are calling data in the constructor when idd is still "" which results in the text box still being empty. Changing the MyParent property does not change that. Only passedv does. But at that point you do not have the parent set. Just call data in passedv, too.
Try this:
public partial class GroupsItems : UserControl
{
//properties and methods
private string idd="";
public string IDD
{
get{return idd;}
set{
idd=value;
textBox1.Text=idd;
}
}
//other properties and methods
}
Usage:
In your Main form:
abc = new GroupsItems();
abc.IDD="sometext";
MainGrid1.Children.Add(abc); //Grid or any other container for your UserControl
In your Binding example, your GroupItem class looks ok, except that you need to pass in the name of the changed property:
public string Text
{
get { return _text; }
set
{
if (value != _text)
{
_text = value;
NotifyPropertyChanged("Text");
}
}
}
Now, in GroupsItems, you shouldn't be accessing the TextBox. In WPF, we manipulate the data, not the UI... but as we use Binding objects to data bind the data to the UI controls, they automatically update (if we correctly implement the INotifyPropertyChanged interface).
So first, let's add a data property into your code behind (which should also implement the INotifyPropertyChanged interface) like you did in your GroupItem class:
private GroupItem _item = new GroupItem();
public GroupItem Item
{
get { return _item; }
set
{
if (value != _item)
{
_item = value;
NotifyPropertyChanged("Item");
}
}
}
Now let's try using a Binding on a TextBox.Text property:
<TextBox Text="{Binding Item.Text}" />
See how we bind the Text property of your GroupItem class to the TextBox.Text property... now all we need to do is to change the value of the Item.Text property and watch it update in the UI:
<Button Content="Click me" Click="Button_Click" />
...
private void Button_Click(object sender, RoutedEventArgs e)
{
Item.Text = "Can you see me now?";
}
Alternatively, you could put this code into your passedv method if you are calling that elsewhere in your project. Let me know how you get on.
UPDATE >>>
In your GroupItem class, try changing the initialization to this:
private string _text = "Any text value";
Can you now see that text in the UI when you run the application? If not, then try adding/copying the whole Text property into your code behind and changing the TextBox declaration to this:
<TextBox Text="{Binding Text}" />
If you can't see the text value now, you've really got problems... you have implemented the INotifyPropertyChanged interface in your code behind haven't you?

C# WPF Datagrid doesn't dynamically sort on data update

I'm having some trouble with my datagrid :
When I update some data (from the model) it shows on my DataGrid, but if click on a header to sort the column it begins to go sideways when I update existing data.
Here's 2 1 examples :
If I add a new value, it doesn't appear at the end (like it does when I don't sort the datagrid) but it shows at the wrong place (same place every time).
If I update an existing value, the order never changes when it needs to do so.
I've seen multiple answers but some say DataGridTextColumn shouldn't be a problem... So I was wondering if it was because of the dictionary...
I know that the first problem with new data had something to do with the dictionary.
Object.
public class Player : INotifyPropertyChanged
{
private string _id;
private string _name;
private int _damage;
private int _heal;
private int _dps;
private int _hps;
private int _time = 1;
public Player(string id, string name)
{
_name = name;
_id = id;
}
public event PropertyChangedEventHandler PropertyChanged;
public string Id
{
get { return _id; }
private set
{
_id = value;
}
}
public string Name
{
get { return _name; }
private set
{
_name = value;
NotifyPropertyChanged("Name");
}
}
public int Damage
{
get { return _damage; }
set
{
_damage = value;
NotifyPropertyChanged("Damage");
Dps = _damage / _time;
}
}
public int Heal
{
get { return _heal; }
set
{
_heal = value;
NotifyPropertyChanged("Heal");
Hps = _heal / _time;
}
}
public int Dps
{
get { return _dps; }
private set
{
_dps = value;
NotifyPropertyChanged("Dps");
}
}
public int Hps
{
get {return _hps; }
private set
{
_hps = value;
NotifyPropertyChanged("Hps");
}
}
public int Time
{
get { return _time; }
set
{
_time = value;
Dps = _damage / _time;
Hps = _heal / _time;
}
}
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
ObservableCollection
public sealed class ShortList
{
private static readonly ShortList instance = new ShortList();
private ObservableCollection<Player> playerList = new ObservableCollection<Player>();
private ShortList()
{
}
public static ShortList getShortList
{
get
{
return instance;
}
}
public ObservableCollection<Player> getPlayerList
{
get
{
return playerList;
}
}
public void updatePlayer(string id, string name, int damage, int heal, int time)
{
Player player;
player = findPlayer(id);
if (player != null)
{
player.Damage = player.Damage + damage;
player.Heal = player.Heal + heal;
player.Time = player.Time + time;
}
else
{
player = new Player(id, name);
player.Damage = damage;
player.Heal = heal;
player.Time = time;
playerList.Add(player);
}
}
public void clear()
{
playerList.Clear();
}
private Player findPlayer(string id)
{
foreach (Player p in playerList)
{
if (p.Id == id)
{
return p;
}
}
return null;
}
}
XAML
<DataGrid AutoGenerateColumns="False"Name="playerDataGrid" IsReadOnly="True" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Header="Nom" Binding="{Binding Name}" MinWidth="35"/>
<DataGridTextColumn Header="Degats" Binding="{Binding Damage}" MinWidth="45" />
<DataGridTextColumn Header="DPS" Binding="{Binding Dps}" MinWidth="29" />
<DataGridTextColumn Header="Soins" Binding="{Binding Heal}" MinWidth="35" />
<DataGridTextColumn Header="HPS" Binding="{Binding Hps}" MinWidth="29" />
</DataGrid.Columns>
</DataGrid>
Code behind the window
public partial class MiniParser : Window
{
public MiniParser()
{
InitializeComponent();
playerDataGrid.ItemsSource = ShortList.getShortList.getPlayerList;
temporyFill();
}
private void temporyFill()
{
ShortList.getShortList.updatePlayer("1234", "ABCD", 100, 0, 2);
ShortList.getShortList.updatePlayer("1234", "ABCD", 100, 0, 0);
ShortList.getShortList.updatePlayer("123", "ABC", 50, 0, 1);
ShortList.getShortList.updatePlayer("234", "ABC", 0, 50, 1);
ShortList.getShortList.updatePlayer("345", "BCD", 1000, 25, 25);
ShortList.getShortList.updatePlayer("456", "CDE", 250, 0, 25);
}
private void startMI_Click(object sender, RoutedEventArgs e)
{
ShortList.getShortList.updatePlayer("5678", "BABA", 100, 100, 100);
ShortList.getShortList.updatePlayer("1234", "ABCD", 100, 0, 0);
}
}
Of course most of the code behind is there for testing purpose... But the idea is that the model is being updated and the view needs to reflect the changes (Even the sorting).
You're not raising the PropertyChanged event when a property updates. This means that a new item which has name changed from "" to "Test", or an existing item that changes, is not going to raise a PropertyChanged event to let the UI know a value has changed and it needs to update.
To fix it, make your properties raise the PropertyChanged event when they change.
public class Player : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
RaisePropertyChanged("Name");
}
}
}
Also, I was re-reading your question and I have no idea what you're doing with ObservableDictionary, but I'd recommend changing that so your DataGrid binds directly to an ObservableCollection<Player>. I suspect that's part of your problem.
var playerCollection = new ObservableCollection<Player>();
playerCollection.Add(new Player { Name = "Test 1" });
playerCollection.Add(new Player { Name = "Test 2" });
playerCollection.Add(new Player { Name = "Test 3" });
playerDataGrid.ItemsSource = playerCollection;
I found a solution, so far so good :
The problem was that ObservableCollection doesn't trigger the event "CollectionChanged" when you change the value of a field in an object which is already in your ObservableCollection. (Possibly because you don't change the reference to it).
Here's an example :
ObservableCollection<MyObject> myCollection = new ObservableCollection<MyObject>();
myCollection.add("CollectionChanged","Yes");
//Let's say I had a method to find a object with the first string and to change the second string
myCollection.change("CollectionChanged", "No");
As you can guess, the second part when I changed a field of my existing object the CollectionChanged didn't trigger...
So the solution I implemented is the following :
class ObsCollection<T> : ObservableCollection<T>
{
public void UpdateCollection()
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Reset));
}
}
All you have to do is create the new type of collection and then when you change a field value of an existing object, call the UpdateCollection() method.
This solution is from Wu Xuesong.
I'd like to say Rachel as been also a big help.
Move any code that looks like this:
this.NotifyPropertyChanged("somepropertyname");
into the setter methods of your properties. That's what the setters are there for.
Also I second the answer suggestig you use an ObservableCollection<T> instead of your ObservableDictionary<TKey,TValue>. They are very common for WPF bindings.

Categories