May be my formulating of this question is incorrect ("How to bind to get-only property of API class instace?") but here's my problem:
I'm creating powerpoint vsto add-in and I need to bind the SlideIndex property of concrete slide to textbox on the windows form. SlideIndex property has only get accessor. I found that in case of binding I need to use mvvm. According to examples on mvvm theme I installed MvvmLightLibs to my solution from NuGet Packege Manager and desided to "wrap" slide object with this way:
public class SlideWraper: ViewModelBase
{
private PowerPoint.Slide Sld;
public int SlideIndex
{
get
{
return Sld.SlideIndex;
}
set
{
RaisePropertyChanged(() => Sld.SlideIndex);
}
}
public SlideWraper(PowerPoint.Slide sld)
{
Sld=sld;
}
}
Here's my code of binding creation:
...
PowerPoint.Slide ConcreteSlide=this.Application.ActivePresentation.
Slides.FindBySlideID(257);
SlideWraper MyWraper=new SlideWraper(ConcreteSlide);
MyTextBox.DataBindings.Add(new Binding("Text", MyWraper, "SlideIndex"));
...
But this realization fills textbox with correct slide index only at the start of the program. When I replace slide (slide index changed) MyTextBox.Text is not changed.
How can I bind to get-only property of slide?
A few options here. If PowerPoint.Slide supports INPC then you should expose it directly...
private PowerPoint.Slide Sld;
public PowerPoint.Slide Slide
{
get {return Sld;}
set {this.Sld = value; RaisePropertyChanged(() => Slide);}
}
... and then in XAML bind to Slide.SlideIndex.
If PowerPoint.Slide doesn't support INPC then you'll need to create a regular int property in your view model and arrange for PowerPoint.Slide to update that property in response to its SlideIndex value changing.
Finally if you know that SlideIndex has just updated then you can just call `RaisePropertyChanged("Slide")', this can be slow because it will cause all bindings to the Slide properties to update, but sometimes it's the only choice.
Either way, automatic updates can't just magically work by themselves, PowerPoint.Slide needs to have some mechanism to notify the rest of the program that its SlideIndex property has changed.
Related
I Created a custom UserControl using Windows Form Control Library.And I want to create a property of UserControlwhich I can add item to it, then I can select item like comboBox.
WinForms allows you to create a rich design-time environment as well as providing for customised editors at runtime for certain properties that you define.
For example, if I plonk a MessageQueue component onto my WinForms form and view the Properties window, I can see a property named Formatter.
Clicking on the Formatter property however displays a drop-down box showing a preset list of values. This is an example of a UI Type Editor.
One way to do this is to define an enum for your supported values (it could be a dynamic list if you wish).
public enum Muppets
{
Kermit,
MissPiggy,
Fozzie
}
...then after defining your own editor derived from UITypeEditor (see MSDN link below)
class MyMuppetEditor : UITypeEditor { ... }
...you attach it to your control's property that you wish to have a drop-down as so:
[Category("Marquee")]
[Browsable(true)]
[EditorAttribute(typeof(MyMuppetEditor),
typeof(System.Drawing.Design.UITypeEditor))]
public Muppets Muppet {get ; set; }
For more detailed information check out the link below.
More
Walkthrough: Implementing a UI Type Editor
Getting the Most Out of the .NET Framework PropertyGrid Control
EDIT: To allow for dynamic list, try making the property a string because that's what the selection will be bound to and during EditValue() when showing your SelectionControl just display a listbox of your dynamic items
You can do this by using the CategoryAttribute class.
Example:
[Description("Description of property here"), Category("Design")]
public bool my_property;
Check out the MSDN page for a more complete reference on how to use it.
EDIT: In the case of wanting to have a bool property, use this example.
private bool my_bool = true; // this is its default value
[PropertyTab("Property Tab Name")]
[Browsable(true)]
[Description("Description of Property"), Category("Data")]
public bool my_property
{
get { return my_bool; }
set { my_bool = value; }
}
I removed my last answer because I misunderstood your point.
An easy solution would require to make a Collection of enum as a property. The Designer property grid will automatically give you the choice among your initialized Collection with a ComboBox. The displayed names will also be the enum's name.
E.g. (something I made for a TextBox that only allows a certain type of value)
The enum :
enum EnumSupportedType
{
Integer = 1,
Double
}
The class where the property is located :
public class NumericTextBox : TextBoxBase, INumericControl
{
private EnumSupportedType _supportedType = EnumSupportedType.Integer;
public EnumSupportedType SupportedType {
get { return _supportedType; }
set { _supportedType = value; }
}
}
Then these items are suggested in a ComboBox (in the Designer property grid) :
Integer
Double
If you can't use enumerations, you can refer to Providing a Custom UI for Your Properties which seems to be a much harder solution to implement but will solve your problem.
I hope it will help you.
I have a ListView and a GridView that lists users in an application by names. Whenever the user selects an user to edit, I add a new tab to a TabControl, and bind all editable properties to the WPF controls.
However, when the user is editing in the Edit Tab, the information in the List (specifically, the name field) is also being updated.
Currently I'm making a copy of the object to be edited and leaving the original so it doesn't update the ListView, but isn't there a better/easier way to do this?
I've tried setting the Binding Mode=OneWay, didn't work, and also UpdateSourceTrigger=Explicit in the GridView but also didn't work.
Is there any easier way to do this?
Edit: The way I implemented my INotifyPropertyChanged class is part of the issue, since I have this:
public partial class MyTabControl : UserControl
{
public MyTabControl()
{
InitializeComponent();
//Here, DataContext is a List<Users>
//Users being my Model from the Database
//Some of it's properties are bound to a GridView
//User doesn't implement INPC
}
public void OpenTab(object sender, RoutedEventArgs e)
{
User original = (sender as Button).DataContext as User;
// - This will create a new ViewModel below with the User I'm sending
MyTabControl.AddTab(original);
}
}
And my ViewModel of Users is:
public class UserViewModel : INotifyPropertyChanged
{
public User Original { get; private set; }
public string Name { get { return Original.Name; } set { Original.Name = value; OnPropertyChanged("Name"); } }
public UserViewModel(User original)
{
Original = original ?? new User();
}
// - INPC implementation
}
Since my ViewModel is the one reporting the property changes, I didn't expect my original User to report it as well to the GridView.
The Mode=OneWay causes the information flow to go from the bound data entity to the target UI property only, any change to the UI property will not be bound back.
The reason why the UI content is changing is because the underlying property is read/write (i.e. has a getter and a setter) and is notifying any value change (due to the implementation of the INPC interface).
Presuming that it is a list of User objects you've bound to the GridView, you have two simple options to fix this. Which one is best depends on how much scope for change you have:
change the current Name property on the User object, remove the setter for it. Replace the setter with a method to set the property (i.e. SetUserName(string name)) which then sets the private member variable. Or pass the name as an argument to the constructor of the User entity.
create a new property with only a getter which returns the name and set your binding to that; i.e. public string UserName { get { return Name; }}. As there is only a getter there will be no notification of this property, so if the name does change it won't be propagated via this new property.
Does mvvmcross natively support binding to nested properties?
For example I have a view model as follows:
class MainViewModel : MvxViewModel
{
public SubViewModelBase SubViewModel
{
get { return _subViewModel; }
set { _subViewModel = value; RaisePropertyChanged(() => SubViewModel); }
}
}
The sub view model may change, but the MainView will bind to the same properties for ALL SubViewModelBase classes... an example SubViewModelBase class as follows:
class SubViewModelBase : MvxViewModel
{
public bool ShowIndeterminantProgress
{
get { return _showIndeterminantProgress; }
set { _showIndeterminantProgress = value; RaisePropertyChanged(() => ShowIndeterminantProgress);}
}
// ... More common properties ...
}
So the MainView would ideally bind like this
this.CreateBinding(_progressBar)
.For(view=> view.Visibility)
.To<MainViewModel>(vm => vm.SubViewModel.ShowIndeterminantProgress)
.WithConversion("Visibility")
.Apply();
Should this work? It doesn't appear to be working, but there are no binding errors in the output?
Does mvvmcross natively support binding to nested properties?
Yes
Should this work?
Yes
For example, this line in ApiExamples for Xamarin.iOS is working:
set.Bind(errorLabel2).To(vm => vm.Errors.Count);
https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/ApiExamples/ApiExamples.Touch/Views/FirstView.cs#L361
The set of supported functionality is described in https://github.com/MvvmCross/MvvmCross/wiki/Databinding#fluent - but admittedly this fluent binding is more established/used in Xamarin.iOS than it is in Wpf.
To try to debug why your current binding might not be working try adding a simple property to your view which provides debug output
private Visibility _mockVisibility;
public Visibility MockVisibility
{
get
{
return _mockVisibility;
}
set
{
Debug.WriteLine("MockVisibility called with " + value);
_mockVisibility = value;
}
}
and binding this as:
this.CreateBinding(this)
.For(view=> view.MockVisibility)
.To<MainViewModel>(vm => vm.SubViewModel.ShowIndeterminantProgress)
.WithConversion("Visibility")
.Apply();
This should give you debug/trace output and should give you somewhere to put a breakpoint to understand the call stack a little too (although expect this to contain lots of reflection which can be hard to read through).
Beyond this:
you could also try binding a label's text to see what that displays.
you can also try setting the binding trace level to Diagnostic (using the static field MvxBindingTrace.TraceBindingLevel https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding/MvxBindingTrace.cs#L14)
try isolating the problem piece by piece (isolating the converter, isolating the nested property, etc, etc) - I guess this is what you are already doing in asking this question.
I've been using the MVVM pattern for a little while now and frequently run into a scenario where the value of one property depends on the value of another property. For instance, I have a control with a height and width, and I want to display the height and width on the control as a formatted string, "{height} x {width}". So I include the following properties in my view model:
public class FooViewModel : INotifyPropertyChanged
{
// . . .
private double _width;
public double Width
{
get { return _width; }
set
{
if(_width != value)
{
_width = value;
NotifyPropertyChanged("Width");
NotifyPropertyChanged("DisplayString"); // I had to remember to
// do this.
}
}
}
public string DisplayString
{
get
{
return string.Format("{0} x {1}", _width, _height);
}
}
// . . .
}
Then I bind to content of my Label to the DisplayString property, which seems a lot more convenient than using a IMultiValueConverter to convert from the Width and Height properties. The inconvenient part is that anywhere I need to NotifyPropertyChanged for "Width" or "Height", I also have to remember to notify for "DisplayString". I can think of myriad ways to automate this, to varying degrees, but my question is whether there is a standard practice that people generally use to do this under MVVM in WPF?
No there is no standard way of doing this.
You can write a base viewmodel class which has PropertyChanged helper method. It will look at the properties of class with DependsOn attribute (which also you'll create) and fire event for all the properties that depend on the updated property.
If there are many dependencies between properties in the ViewModel, you can invoke NotifyPropertyChanged with an empty string to refresh all the elements which are bound from the View. I don't see a point in automating this.
I've made a C# usercontrol with one textbox and one richtextbox.
How can I access the properties of the richtextbox from outside the usercontrol.
For example.. if i put it in a form, how can i use the Text propertie of the richtextbox???
thanks
Cleanest way is to expose the desired properties as properties of your usercontrol, e.g:
class MyUserControl
{
// expose the Text of the richtext control (read-only)
public string TextOfRichTextBox
{
get { return richTextBox.Text; }
}
// expose the Checked Property of a checkbox (read/write)
public bool CheckBoxProperty
{
get { return checkBox.Checked; }
set { checkBox.Checked = value; }
}
//...
}
In this way you can control which properties you want to expose and whether they should be read/write or read-only. (of course you should use better names for the properties, depending on their meaning).
Another advantage of this approach is that it hides the internal implementation of your user control. Should you ever want to exchange your richtext control with a different one, you won't break the callers/users of your control.
Change the access modifier ("Modifiers") of the RichTextBox in the property grid to Public.
Add a property to the usercontrol like this
public string TextBoxText
{
get
{
return textBox1.Text;
}
set
{
textBox1.Text = value;
}
}
I recently had some issues doing this with a custom class:
A user control had a public property which was of a custom class type. The designer by default tries to assign some value to it, so in the designer code, the line userControlThing.CustomClassProperty = null was being automatically added.
The intent was to be able to provide the user control with a custom class at any point while running the program (to change values visible to the user). Because the set {} portion did not check for null values, various errors were cropping up.
The solution was to change the property to a private one, and use two public methods to set and get the value. The designer will try to auto-assign properties, but leaves methods alone.
You need to make a public property for the richtextbox, or expose some other property that does the job of setting the richtextbox text like:
private RichTextBox rtb;
public string RichTextBoxText
{
get
{
return rtb.Text;
}
set
{
rtb.Text = value;
}
}