C# WPF Switch Resource Programmatically - c#

i`m trying to design a dialog window which will display different messages depending on what case will be true.
so for example when i have a
<CustomDialog Content="{Binding Path=Name}"/>
is there a possibility to change the Binding Path=Name to Path=Whatever or how do you implement something like that ? When the Control should use other resources on runtime.
--------------edit
I ok i`ll try to describe my problem better ;)
I have an ResourceDictionary with strings for example
<System:String x:Key="Message1">Message1</System:String>
<System:String x:Key="Message2">Message2</System:String>
<System:String x:Key="Message3">Message3</System:String>
So when I now call my UserControl
Doing it customdialog.visibility = true; for example
<CustomDialog Text=”” />
I want to define which key from the resourcedictionary is taken when the dialog popups up.
something like customdialog.text = Message1; but Loaded from the ResourceDictionary
is that possible or is there an better way of doing something like this ?

You may provide another content to the same property Name at runtime in code-behind. Suppose you have Initialize (or may be Show) method in your CustomDialog and the last one implements INotifyPropertyChanged:
public class CustomDialog : INotifyPropertyChanged
{
//Your implementation of class goes here
public void Initialize(string message)
{
Name = message;
Visibility = Visibility.Visible;
}
public string Name
{
get {return _name;}
set
{
if (_name != value)
{
_name = value;
raiseOnPropertyChanged("Name");
}
}
}
//Your implementation of class goes here
}
In method Initialize there will be updated Name property and your control will be shown. When there will be setting of Name property must be raise PropertyChanged event which will tell presentation that binded value has updated and to reflect it in the UI.

The easiest way I can think of would be to bind to the parent item, not to a child property, and then use a DataTemplateSelector to select a different template at run-time, depending on some condition involving the bound object or its properties.
Alternatively, if the Content has well defined types, you only need to define DataTemplates with specific data types, and they will be automatically used to display objects of those types.
Not knowing more about the context I can't be much more specific, but if you search for more information on DataTemplates and DataTemplateSelectors you should be fine - you can find a lot of useful information here.

Related

wpf button visibility in case of CommandParameter

I need to make few buttons like this:
<fluent:Button
Size="Middle"
Visibility="{Binding Path=SomeTestingMethod}"
Command="{Binding Path=OtherMethod}" CommandParameter="PP"
Some Text</fluent:Button>
visible or not in case of "CommandParameter". I tried:
public Visibility SomeTestingMethod(object o)
{
return o.ToString == "something" ? Visibility.Visible : Visibility.Collapsed;
}
But compiler do not even check it. Also tried stuff like this:
private Visibility _someTestingMethod;
public Visibility SomeTestingMethod
{
get {
var commandExecutor = new RelayCommand(ButtonVisibility);
return _statusButtonsVisibility;
}
}
public void ButtonVisibility(object o)
{
_statusButtonsVisibility =
o.ToString == "something" ? Visibility.Visible : Visibility.Collapsed;
}
"SomeTestingMethod" is then reached but "ButtonVisibility" not.
I Have found other ways to reach visibility, but none of them alows me to get CommandParameter.How to do it correctly?
I have a few comments about the code presented.
First off, do you really want to make the button disappear if the user may not click it? I ask because the ICommand interface has a CanExecute() method which can hold logic to determine if the command may be executed. When a button is bound to a property that is an instance of an object implementing the ICommand interface, the button will automatically enable/disable itself based on the results of the CanExecute() logic. Note, that if that logic does something on a different thread, you may have to force a re-query of the command availability.
If you truly want the button to disappear rather than being disabled, as mentioned by #Jason Boyd in the comments, this is best accomplished by binding the visibility to a Boolean property in the view model and using a BooleanToVisibilityConverter to show/hide the button based on true/false of the property.
The view model should implement the INotifyPropertyChanged interface to communicate property changes to update the binding target.
Hopefully, that gives you a start in the right direction.
You can't get CommandParameter in property which you bind to Visibility property.
Get parameter in OtherMethod method and change SomeTestingMethod property.
Or you can use custom BoolToVisibility converter for using parametr.

Why do properties show they are NOT public?

I have a Label I am trying to bind to a property. My DataContext is a public Class that implements INotifyPropertyChanged.
My property is defined in my public class like this:
public string ProgressMessage
{
get { return _progressMessage; }
set
{
if (_progressMessage != value)
{
_progressMessage = value;
NotifyPropertyChanged("ProgressMessage");
}
}
}
So why in the world does it show up in intellisense like this (NOT public):
Is this why does my label not update when I change the value of ProgressMessage??
I have done the Mode=TwoWay, UpdateSourceTrigger=PropertyChanged and every other combination I can think of, can anyone help or point me in the right direction?
It seems to me that the symbol you see that kind of looks like a lock is not actually a lock. Instead it seems to be identifying properties on the data context that you can to bind to. I was able to recreate this issue and successfully binded to a property that had the symbol.
If you go ahead and type {Binding Path= You will see that it only shows you possible binding paths:

WinForms - how to save the combobox selection in the app's settings?

I have a combobox embedded in a toolstrip - a ToolStripCombobox instance.
The list of items is the list of values of an enum.
I'd like to be able to load/save the selection (One of the Selected[Index|Item|Text|...] properties, from/to the app's Settings "mechanism".
Ideally, I'd like to be able to do that from the designer.
Normally, hooking a control's property to a certain setting is done (in the designer) from the control's properties, under (ApplicationSettings) - but none of the SelectedXXX properties shows up in there.
FWIW, in the particular case of toostrip-bound combo-boxes, the actual SelectedXXX properties are actually found a bit deeper, at toolStripComboInstance.ComboBox.SelectedXXX.
What I have done so far is configure the binding in code:
m_runTypeCombo //the toolstrip control
.ComboBox //the actual combobox
.DataBindings.Add(
new System.Windows.Forms.Binding(
"SelectedItem",
global::JavaPad.Properties.Settings.Default,
"RunType",
true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged
)
);
The above works, but I was hoping for something cleaner (i.e. designer-based). If the built-in ToolStripCombobox doesn't support this, is there a (simple) way to derive my own type from that, and expose the SelectedXXX properties in such a way that it works with the Application Settings infrastructure (and its support in the designer)?
If you are willing to wrap the TooltipComboBox in your own custom control, you can do it like this:
public class MyCombo : ToolStripComboBox
{
[SettingsBindable(true)]
public int SelectedIndex
{
get { return ComboBox.SelectedIndex; }
set { ComboBox.SelectedIndex = value; }
}
}
Note that I haven't tested this beyond confirming that I can add the control to the ToolStrip, and that I can select a property - You may need to add PropertyChanged Events to make it work fully.

Inherit properties into custom control C#

I have a custom control that I have created with a bunch standard windows asp controls on it.
Question:
Is it possible to inherit the properties of the parent control into the custom control without re-inventing the wheel?
So for example I have a control with a Button, a TextBox, and a Label.
Normally I can access the properties of that control via Lable1.Text however when these controls are places within a custom control how do I access them without encapsulating all the properties of that control individually.
I was hoping for something like CustomControl1.Lable1.Text or is this not possible
If I use this
public Label lbMovieName
{
get { return this.lbMoveName; }
set { lbMovieName = value; }
}
I get what I need but can you please tell me why I should not do it?
The easiest way is to expose the control through a public read-only property:
public Label MyLabel
{
get { return this.Label1; }
}
However encapsulating just the values you want to expose is definitely a cleaner solution for several reasons:
you can abstract away that actual control type versus being tied to a Label in this case - if you expose the control it will be difficult to swap out the Label with MyNewCoolLabel, for example
You may be exposing more that you want to - the client could change the display properties of the label, etc.
If you are trying to avoid creating properties you can make the controls public (this is not sound OO development). As others have already mentioned you'd be much better served exposing the information that you'd want to share via properties.
The best way and the best practice, I think, is to create properties of your custom control that expose only and exactly what you need. Everything else inside your control should remain private. Something like this:
public string LabelText {
get { return this.Label1.Text; }
set { this.Label1.Text = value; }
}
... and so on for the rest of the properties you need exposed. This will give you nice intellsense response in the designer as well.

WPF Databound Property Loosing value

Hi I have a viewmodel where i can track the value of a certain item in the constructor. I am opening a dialog window using the MVVM model.
example
private int _myField;
public ClassName(int MyProperty)
{
_myField = MyProperty;
}
public int MyIntProperty
{
get{ return _myField;}
set { _myField = value;}
}
this is all perfect obviously.
but as soon as the window opens the value in the viewmodel changes.
lets say the _myField goes from 1 to 8 with out any interaction. i've walked through the code and there are no other interactions with the field.
also not in the code sample is the bound property.
anyone every came accross this before. it has me stumped.
Edit: included missing property from example
You should either:
1) Implement INotifyPropertyChanged on ClassName. This will allow you to raise the PropertyChanged event when you change MyIntProperty. WPF will listen to this event and update the UI accordingly.
or
2) Make ClassName inherit from DependancyObject and MyIntProperty a dependency property. This will take care of everything for you.

Categories

Resources