WPF services for YES/NO MessageBox - wpf

With reference to What exactly are "WPF services"? and its attachment https://digitaltapestry.wordpress.com/2009/07/21/services-%E2%80%93-your-viewmodel-death-star/ .
Using services in WPF MVVM application, Can i use this type of service in ViewModel even to display MessageBox with Yes No Cancel buttons?
If yes, whitch data type should i return in ViewModel (Boolean Yes=> true No/Cancel => false) (MessageBoxResult)?

Can i use this type of service in ViewModel even to display MessageBox with Yes No Cancel buttons?
Yes.
If yes, whitch data type should i return in ViewModel (Boolean Yes=> true No/Cancel => false) (MessageBoxResult)?
The service should return a bool?, .e.g.:
public class DisplayMessageService : IDisplayMessage
{
public bool? ShowDialog(string message)
{
MessageBoxResult result = MessageBox.Show(message, "title...", MessageBoxButton.YesNoCancel);
switch (result)
{
case MessageBoxResult.Yes:
return true;
case MessageBoxResult.No:
return false;
default:
return null;
}
}
}
If you simply display a MessageBox without any "Yes" or "No" button, you shouldn't return anything from the method.

Related

ReactiveUI, can execute doesn't disable button in WinForm

I got a simple WinForm application with a couple of textboxes and a confirm button, I'm using ReactiveUI.
This is my ViewModel:
public CurrencyViewModel()
{
editCurrency = new Currency();
this.ValidationRule(
viewModel => viewModel.IsoCode,
isoCode => !string.IsNullOrWhiteSpace(isoCode),
"error");
this.ValidationRule(
viewModel => viewModel.Name,
name => !string.IsNullOrWhiteSpace(name),
"error");
NewCommand = ReactiveCommand.Create(() => NewItem());
SaveCommand = ReactiveCommand.Create(() => Save(), this.IsValid());
}
public string IsoCode
{
get => isoCode;
set
{
editCurrency.IsoCode = value;
this.RaiseAndSetIfChanged(ref isoCode, value);
}
}
public string Name
{
get => name;
set
{
editCurrency.Name = value;
this.RaiseAndSetIfChanged(ref name, value);
}
}
private void NewItem()
{
IsoCode = string.Empty;
Name = string.Empty;
Symbol = string.Empty;
}
I then bind my validation and my save command in the view:
this.BindValidation(ViewModel, vm => vm.IsoCode, v => v.errorLabelIsoCode.Text).DisposeWith(disposables);
this.BindValidation(ViewModel, vm => vm.Name, v => v.errorLabelName.Text).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.SaveCommand, v => v.sfButtonOk, nameof(sfButtonOk.Click)).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.NewCommand, v => v.sfButtonNew, nameof(sfButtonNew.Click)).DisposeWith(disposables);
My issue is that sfButtonOk stays enabled when i first launch the application even if isValid() is false, the command doesn't fire as intended so it's just a grapichal problem it seems. The button is disabled only if I write valid text and then cancel it.
It seems that the button is disabled only when isValid goes from true to false
The issue here is probably related to a view model being initialized too late, or due to the view model property not sending change notifications on the view side in time. Make sure you assign the view model to the IViewFor.ViewModel property before a call to WhenActivated, or otherwise implement INotifyPropertyChanged on the view side (also you probably don't need a WhenActivated at all because WinForms doesn't have dependency properties that might introduce a potential for a memory leak)
Additionally worth noting, that we have evergreen sample apps targeting various UI frameworks including Windows Forms in the ReactiveUI.Validation core repository https://github.com/reactiveui/ReactiveUI.Validation/blob/d5089c933e046c5ee4a13149491593045cda161a/samples/LoginApp/ViewModels/SignUpViewModel.cs#L43 Just tested the Winforms sample app, and the button availability seems to perform as we'd expect.

javafx combobox contextmenu not displayed

my problem is:
i made a combobox and i want to use context menu on it's elements, so when i'm setting the cellfactory as shown below, i can't see the items in any more and the context menu does not show.
CBXGroups.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
public ListCell<String> call(ListView<String> param) {
final ListCell<String> cell = new ListCell<String>();
final ContextMenu cellMenu = new ContextMenu();
MenuItem rimuoviDalControllo = new MenuItem("RIMUOVI DAL CONTROLLO");
MenuItem rimuoviDefinitivamente = new MenuItem("RIMUOVI DEFINITIVAMENTE");
rimuoviDalControllo.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Service.deleteGroupFromControl(cell.getText(),CBXControllo.getSelectionModel().getSelectedItem());
populateLists();
}
});
rimuoviDefinitivamente.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Service.deleteGroup(cell.getText());
populateLists();
}
});
cellMenu.setOnShowing(new EventHandler<WindowEvent>() {
public void handle(WindowEvent event) {
cell.requestFocus();
}
});
cellMenu.getItems().addAll(rimuoviDalControllo,rimuoviDefinitivamente);
cell.contextMenuProperty().bind(Bindings.when(Bindings.isNotNull(cell.itemProperty())).then(cellMenu).otherwise((ContextMenu) null));
return cell;
}
});
You can't see the items because you haven't set the text in your ListCell. You can do this with a one-liner:
cell.textProperty().bind(cell.itemProperty());
The context menu is trickier, and I don't really have a solution for it. The issue is that the ComboBox uses a PopupControl to display the list view, and the popup control has autoHide set to true. So when you click on the list view, the popup closes (preventing you seeing the context menu). There's no way to access the popup control, so I don't think there's going to be any way to do this.
Registering a context menu with items in a combo box seems like an unusual thing to do; I wonder if there is a better approach for what you want to do. A MenuButton is similar to a ComboBox in some ways (control that displays a popup with options), but it has a menu hierarchy so you can include cascading menus. This might provide the kind of functionality you want.

Issue with focus element in GotFocus/Activated event

There is form with text element that should receive focus every time form shown.
In .NET CF Form has no OnShow(n) event
I try to use workaround:
MyForm_GotFocus() // or MyForm_Activated
{
txtTextControl.Text = string.Empty;
txtTextControl.Focus()
}
txtTextControl_GotFocus()
{
txtTextControl.SelectAll()
}
Code for getting form instance:
public static MyForm GetForm
{
get
{
if (s_inst == null || s_inst.IsDisposed)
{
s_inst = new MyForm();
}
return s_inst;
}
}
public static void ShowForm()
{
var frm = GetForm;
frm.Show();
}
1) First time ShowForm (Form instance has been created): txtTextControl emptied and got focus, txtTextControl_GotFocus event raised
2) Second time ShowForm : OK too
3) Third time ShowForm : txtTextControl emptied, but does not get focus
Is there bug or feature? Is there workaround? Show I rewrite ShowForm? Is OpenNETCF.IOC.UI is better solution (50 forms in project)?
I had that same question once.
Set the TabIndex for the control to 0.

Extjs set readOnly option when button clicked

Good day!
I am try to write method, which set(true or false) readOnly option on the elements on form, when button clicked:
Ext.override(Ext.form.Panel,{
setReadOnly: function(bReadOnly) {
this.items.each(function(f){
if (f instanceof Ext.form.FieldSet) {
f.items.each(function (f) {
if (f.isFormField) {
if (bReadOnly === true) {
f.inputEl.dom.setAttribute('readonly', true);
} else {
f.inputEl.dom.removeAttribute('readonly');
}
if (f instanceof Ext.form.TriggerField)
{
f.setDisabled(bReadOnly);
if (f instanceof Ext.form.ComboBox)
{
f.setEditable(bReadOnly);
}
}
}
});
}
});
});
On the textfield this code works perfect. But on the TriggerField i can not show trigger, when set readOnly option in false.Can Anybody help me?
You probably want the TriggerField setReadonly method
See the API reference here: http://docs.sencha.com/ext-js/3-4/#!/api/Ext.form.TriggerField-method-setReadOnly
You shoudln't need to manually alter the DOM, there are ExtJs methods available to do what you require.
Also, you can use the Panel findByType method to get all the form fields inside your panel.
http://docs.sencha.com/ext-js/3-4/#!/api/Ext.form.FormPanel-method-findByType

How to bring a form in the top of desktop?

I want to pop up a form in C# .net 2.0 which should be in front of the desktop (topmost) until the user clicks the close button.
How to do so?
I tried the code from here:
http://dotnet-snippets.de/dns/fenster-wirklich-in-den-vordergrund-des-desktops-bringen-SID1005.aspx
But it didn't work.
My system is Win7.
Set the form's TopMost property to true and MinimizeBox property to false.
Code below will create MessageBox with TopMost property making it on Top until user clicks No or Yes.
DialogResult result = DialogResult.No;
try {
result = MessageBox.Show(new Form {
TopMost = true, MinimizeBox = false,
}, "some text", "some topic", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
} finally {
if (result == DialogResult.No) {
}
}

Resources