I'm relatively new to C# and the idea of data binding (that is, I'm not sure I completely understand it). I am looking to try to bind NavigationPage's BarBackgroundColorProperty for sake of being able to change my root page's bar background color multiple times for a theme feature I'm trying to implement. I've searched around for examples of binding this property and haven't had much luck. Could someone point me in the right direction? If anything is unclear, please let me know. Thanks guys!
Assuming you have an instance of NavigationPage called myBindingPage and a view model that extends INotifyPropertyChanged called myViewModel that has a property called BarBgColor, you should be able to set up a binding like this:
myBindingPage.SetBinding(NavigationPage.BarBackgroundColorProperty, new Binding("BarBgColor"));
N.B. I haven't actually tried this, but the API doc does list BarBackgroundColor as a bindable property.
Related
I hope my Title is not too confusing.
I'm new in MVVM so sorry for some missunderstandings on my side in advance.
I have a Window A in which I put a Custom Control B. B has a ViewModel VM.
in VM i created a Custom Dependenty Property DP.
So no i want to bind DP on a Property in my Window A.
My question is now how do i do that?
Is it even possible?
Or do i have to put my Dependency Property and all other logic in the code behind in this case?
I hope someone can help me and i provided enough information to understand my problem...
You're having issues because you created a ViewModel for your UserConrol. Think about it--does a TextBox have a TextBoxViewModel? No.
Your UserControl should be designed like any other control. Expose properties on its surface that people bind to. You can bind to those properties within the UserControl as needed. And use your codebehind for whatever UI logic you need.
Business logic shouldn't be scoped within a UserControl, so pull any of that out.
I looked at the article at WPF add datagrid image column possible? and it looks fairly straightforward to add an image column to a datagrid. However one part I dont understand is the code line:
textColumn1.Binding = new Binding("YourBindingField");
Something similar appears in the MSDN help for Binding(string).
What does this string mean and where is it coming from? what am I binding to . I am completely lost on this and need help as to where I get this string from. I just have an array of image paths. is there a class i need to build that links them somehow.
Any help appreciated.
"YourBindingField" is the property that you are binding to the column.
It is the property of your data context which can be either a dependency property in your code behind, or a property in the view model.
This line is setting the Binding of the control to the Property "YourBindingField" on the current data context. WPF allows you to bind a controls value of an object that is set to the controls data context or the data context of one of its parent containers.
I have the same porblem every time. I want to create a custom control with "UIElement" DependencyProperty. So, on which element in XAML I could create binding to this DependencyProperty?
Canvas's Children property says that it's impossible, Grid even don't have "Children" property in XAML and so on.
Thank you!
Not sure if this is what you are looking for but javascript knockout works well with UI bindings:
http://knockoutjs.com/
Also take a look at handlebars:
http://handlebarsjs.com/
Hope that helps
Is XAML really necessary? If someone really wanted to, could everything displayed on a Silverlight page be declared at runtime in the C# code?
VISIFire has example code where they set up one of their classes, chart, in the C# code and then simply add it to the user interface with a simple C# call:
LayoutRoot.Children.Add(chart);
How would I do a similar creation with a class I already have defined in the XAML file but because of other reasons I need to declare it at runtime? If I could use it as it is in XAML I think it would work because there are no errors with its display. It is declared like this:
<views:PrescriberPage01 x:Name="DashboardPrescriberPage01"
Visibility="Collapsed" SelectedProduct="{Binding SelectedProduct,
Mode=TwoWay}"Grid.Row ="0"/>
How would I declare that in C# code?
Is XAML really necessary?
I suppose no, you can probably do everything that you can do in XAML in code. But XAML is really, really a much, much more convenient way to go. And I think few would dissagree with this.
How would I declare that in C# code?
You can create bindings in code. Example from MSDN. In your situation it would be something like:
PrescriberPage01 page = new PrescriberPage01();
page.Visibility = Visibility.Collapsed;
Binding myBinding = new Binding("SelectedProduct");
myBinding.Mode = BindingMode.TwoWay;
myBinding.Source = this; // or whatever object carrying the property
// "SelectedProduct" that you bind against.
page.SetBinding(PrescriberPage01.SelectedProduct, myBinding);
LayoutRoot.Children.Add(page);
XAML is preferable to describe user interface, in declarative way and I recommend use XAML without code behind and also I would recommend look at MVVM pattern
I would rather say "Is C# really necessary for GUIs when I can use XAML" ? XAML is really good and was explicitly design for GUI.
You have two options here. Load your XAML dynamicly.
Or create it in code. (other posted the solution while i was typing AGAIN! ;) )
Simple controls are easy but... you might also want some styling done to those objects +
you might want an animation or two... this is already(mostly) provided in xaml ... and it is uber easy... If u hate xaml so much you might want to think about "Blend";p
One specific example of something you can't create without Xaml is a DataTemplate.
You can write code which will create a DataTemplate from a XAML string, but you can't add children to a newed up DataTemplate directly in c#.
I'm trying to create a binding from my custom control to objects that are in a BindingList.
While with textbox, I can easily write
textBox.DataBindings.Add("Text",myBindingList,"PropertyOfObjectOfBindingList")
With my custom property "Value", this thing doesn't work (the object doesn't get updated).
What should I implement with my custom control to make it works? I already implemented INotifyPropertyChanged, but it doesn't work.
I just want make this line works:
customControl.DataBindings.Add("CustomProperty",myBindingList,"PropertyOfObjectOfBindingList")
EDIT 1:
I read this around web: http://kbalertz.com/327413/control-using-Visual.aspx however is not working for me at the moment, maybe I'm doing something wrong
Since you said your bound object doesn't get updated (I assume from Control -> Object changes), but it is bound correctly, maybe this will help:
customControl.DataBindings.Add("CustomProperty", list, "BoundObjectProperty",
false, DataSourceUpdateMode.OnPropertyChanged);
Maybe the Implementing complex data binding in custom controls article will help.
I solved the problem by myself:
While the article I linked is a good suggestion, there is a wrong part; you don't have to create an event in your custom class with PropertyChangedEventHandler, but just with EventHandler.
public event EventHandler CustomPropertyChanged;
Is enough to make everything works. Obviusly you have to call it when your property changes
EDIT 1:
I discovered a bad thing, while on textboxes, if the control lose focus the bindinglist get updated, on my custom controls this thing happens only when I change selected item in listbox.
I don't find a way to solve this at the moment.