The following code works without a problem
using System;
using System.Windows.Forms;
namespace ClassLibrary1 {
using System.Diagnostics;
public class person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
person fred = new person();
private void button1_Click(object sender, EventArgs e)
{
fred.FirstName = "Fred";
this.textBox1.DataBindings.Add("Text", fred, "FirstName");
Debug.Assert(textBox1.Text == fred.FirstName); // true
}
} }
yet when i try a similar patttern to databind a class with many more properties, the assertion fails.
the textbox is blank even though the bound property has data.
what could i be doing wrong?
OOps, the pattern was not the same.
I was setting the DataBindings before the form was visible, and this seems to be the problem.
Related
Thanks in advance!
How should I use ObservesCanExecute in the DelegateCommand of PRISM 6?
public partial class UserAccountsViewModel: INotifyPropertyChanged
{
public DelegateCommand InsertCommand { get; private set; }
public DelegateCommand UpdateCommand { get; private set; }
public DelegateCommand DeleteCommand { get; private set; }
public UserAccount SelectedUserAccount
{
get;
set
{
//notify property changed stuff
}
}
public UserAccountsViewModel()
{
InitCommands();
}
private void InitCommands()
{
InsertCommand = new DelegateCommand(Insert, CanInsert);
UpdateCommand = new DelegateCommand(Update,CanUpdate).ObservesCanExecute(); // ???
DeleteCommand = new DelegateCommand(Delete,CanDelete);
}
//----------------------------------------------------------
private void Update()
{
//...
}
private bool CanUpdate()
{
return SelectedUserAccount != null;
}
//.....
}
Unfortunatelly, I'm not familiar with expressions in c#. Also, I thought this would be helpful to others.
ObservesCanExecute() works “mostly like” the canExecuteMethod parameter of DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod).
However, if you have a boolean property instead of a method, you don't need to define a canExecuteMethod with ObservesCanExecute.
In your example, suppose that CanUpdate is not a method, just suppose that it's a boolean property.
Then you can change the code to ObservesCanExecute(() => CanUpdate) and the DelegateCommand will execute only if the CanUpdate boolean property evaluates to true (no need to define a method).
ObservesCanExecute is like a “shortcut” over a property instead of having to define a method and having passing it to the canExecuteMethod parameter of the DelegateCommand constructor.
In AttachmentViewModel I have the following code
public ICommand EditAttachmentCommand { get; private set; }
public AttachmentsViewModel()
{
EditAttachmentCommand = new ActionCommand<AvailAttachment>(EditAttachment);
}
private void EditAttachment(AvailAttachment attachment)
{
var attachmentDetailsViewModel = Router.ResolveViewModel<AttachmentDetailsViewModel>(true, ViewModelTags.ATTACHMENT_DETAILS_VIEWMODEL);
attachmentDetailsViewModel.NavigateToAttachment(attachment.ArticleId);
EventAggregator.Publish(ViewTags.ATTACHMENT_DETAILS_VIEW.AsViewNavigationArgs());
EventAggregator.Publish(new CurrentViewMessage(ContentView.Attachment));
}
In my MainViewModel I have the following code:
public ICommand SessionAttachmentCommand { get; private set; }
public MainMenuViewModel()
{
SessionAttachmentCommand = new ActionCommand<AvailAttachment>(EditAttachment);
}
private void EditAttachment(AvailAttachment attachment)
{
var attachmentDetailsViewModel = Router.ResolveViewModel<AttachmentDetailsViewModel>(true, ViewModelTags.ATTACHMENT_DETAILS_VIEWMODEL);
attachmentDetailsViewModel.NavigateToAttachment(attachment.ArticleId);
EventAggregator.Publish(ViewTags.ATTACHMENT_DETAILS_VIEW.AsViewNavigationArgs());
EventAggregator.Publish(new CurrentViewMessage(ContentView.Attachment));
}
I would like to pass the object state of AvailAttachment class from AttachmentsViewModel to MainMenuViewModel. presently null value comes for AvailAttachment's attachment object in MainMenuViewModel. I am debugging a code written by someone. This is a silverlight project using MVVM model. How do I do that?
Any help is appreciated. Thanks
i have the below coding in winForms in 3 different btns.
I have very limited knowledge of winForms, as i mostly use Console applications, but i am trying to use text boxes and buttons and user input to create a program that calculates the price variations of a product.
basically i what i am struggling with is converting whatever is in a text box (usually doubles) to a given name to be used to perform calculations on and then append them to the next text box etc.
can anyone help me with this please? it would be much appreciated!
thank you!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Form1 salePrice { get; private set; }
public Form1 discountPrice { get; private set; }
public Form1 vat { get; private set; }
public Form1 onlyVat { get; private set; }
public Form1 totalPrice { get; private set; }
public Form1 changeGiven { get; private set; }
public Form1 payment { get; private set; }
private void calcPriceAndDiscount_Click(object sender, EventArgs e)
{
salePrice = PriceBox;
discountPrice = DiscountBox;
salePrice = (salePrice - discountPrice);
SubtotalBox.AppendText(String.Format("{0:c}", salePrice));
}
private void calcWithVat_Click(object sender, EventArgs e)
{
onlyVat = (salePrice / 100.00 * vat);
totalPrice = (onlyVat + salePrice);
totalPrice = FinalPriceBox;
vat = VATBox;
FinalPriceBox.AppendText(String.Format("{0:c}", totalPrice));
}
private void calcPaymentMinPrice_Click(object sender, EventArgs e)
{
changeGiven = (payment - totalPrice);
payment = PaymentBox;
ChangeGivenBox.AppendText(String.Format("{0:c}", changeGiven));
}
}
}
double dbl;
if (double.TryParse(TextBox1.Text, out dbl))
{
// dbl contains the value of the text
}
else
{
// The text could not be converted to a double
}
When you're done with the value:
TextBox1.Text = dbl.ToString();
Without wanting to bug sacha too much, does anyone know what the Cinch V2 way of closing a View from a ViewModel command?
Previously I have used a RelayCommand in the ViewModel base to accept the Escape keybinding command action and wired up a RequestClose event in the View code behind to do this.
Use CloseActivePopUpCommand.Execute(true) in the execute method to close a view.
I've included a short sample below.
[ExportViewModel("PickOperatorViewModel")]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class PickOperatorViewModel : ViewModelBase
{
[ImportingConstructor]
public PickOperatorViewModel()
{
PickOperaterCommand = new SimpleCommand<Object, Object>(CanExecutePickOperaterCommand, ExecutePickOperaterCommand);
}
public SimpleCommand<Object, Object> PickOperaterCommand { get; private set; }
private void ExecutePickOperaterCommand(Object args)
{
CloseActivePopUpCommand.Execute(true);
}
private bool CanExecutePickOperaterCommand(Object args)
{
return true;
}
}
I've done it one thousand of times and it works but now .... not :(
Am I doing something wrong here because nothing is shown in grid ?
namespace theGridIsNotWorking
{
using System;
using System.Collections.Generic;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var items = new List<Item>();
items.Add(new Item(){ TheName = "first"});
items.Add(new Item(){ TheName = "Second"});
items.Add(new Item(){ TheName = "Third"});
dataGridView1.DataSource = new List<Item>(items);
}
public class Item
{
public string TheName;
}
}
}
Nothing spectaculos .... but really sad.
I think the problem is that TheName is a member variable, but you need a property.
Try the following for the Item class:
public class Item
{
public string TheName;
public string TheNameProperty
{
get
{
return TheName;
}
}
public Item(string name)
{
TheName = name;
}
}
Try BindingListView. Easiest way to bind a List<T> to a DGV.
BindingList<Notification>(notifications);
shouldn't it be
BindingList<Notification>(activeNotifications);
?