How to bind RadDataPager.PageSize to RadNumericUpDown.Value in code - c#

I have a RadGridView, a RadDataPager and a RadNumericUpDown all defined in code.
Now i want to bind the RadDataPager.PageSize to the RadNumericUpDown.Value so the pagesize of the pager is changeable via the RadNumericUpDown control.
Thus i try:
RadDataPager dataPager = new ...;
RadNumericUpDown pageSizeSelector = new ...;
Binding b = new Binding();
b.Mode = BindingMode.TwoWay;
b.Source = pageSizeSelector.Value;
pageSizeSelector.SetBinding(dataPager.PageSize, b);
But this generates an error about the dataPager.PageSize not being a DependencyProperty. What i'm a missing?
EDIT
Thanks to Klinger i got it straight. SetBinding wants the Static Definition of the DP, not a reference to the instance.
Binding b = new Binding("PageSize");
b.Mode = BindingMode.TwoWay;
b.Source = dataPager;
pageSizeSelector.SetBinding(RadNumericUpDown.ValueProperty, b);

Only dependency properties know how to handle binding expressions.
Take a look on the control documentation to see if the PageSize property is a dependency property.

Related

WPF Set a TextBox property in code-behind

Hi I want to set the Text property of a Textbox by code behind. At the moment I do using XAML:
<TextBox x:Name="txtFilter" Text="{Binding FiltroFunzioni, Mode=OneWayToSource}" Grid.Row="0" />
As test I did this:
Binding b = new Binding();
b.Mode = BindingMode.OneWayToSource;
b.Path = new PropertyPath("Text"); //??
b.Source = PageViewModel.FiltroFunzioni;
BindingOperations.SetBinding(txtFilter, TextBlock.TextProperty, b);
The variable "FiltroFunzioni" is a string defined as property:
private string _filtroFunzioni = "";
public string FiltroFunzioni
{
get { return _filtroFunzioni; }
set
{
_filtroFunzioni = value;
RaisePropertyChanged("FiltroFunzioni");
_functionsView.Refresh();
}
}
Basically I dunno what kind of value should I set as PropertyPath. Any ideas?
You don't need the PropertyPath here. If you just remove it, your binding should work.
That being said, you should bind in XAML wherever possible.
If your issue is that changes to FiltroFunzioni don't update your textbox, that's because your binding is specifically declared as OneWayToSource: that means that changing the UI changes the source, but changing the source doesn't change the UI. If that isn't what you want, set the Mode to something else, like "TwoWay" - then changes to the source change the UI, AND changes to the UI change the source.
EDIT:
If you really want to bind from your ViewModel instead of just using XAML, TwoWay binding requires utilizing the Path for some reason, when binding through C#. Either of the following solutions work:
b.Source = FiltroFunzioni;
b.Path = new PropertyPath(".");
b.Source = this;
b.Path = new PropertyPath("FiltroFunzioni");
Note that with TwoWay binding you have to either initialize your FiltroFunzioni by setting the TextBox.Text property in your XAML, or setting FiltroFunzioni after the binding was initialized. Otherwise, WPF will immediately override it from the (by default empty) Text in your TextBox.

How to properly use a binding in C#

I want to bind the background of a WPF Rectangle to the Brush property of another element.
The initialization looks like this:
MazeElement nextElement = new MazeElement();
nextElement.Position = new Point(xIndex, yIndex);
nextElement.BackgroundColor = Brushes.Aqua;
MazeElements.Add(nextElement);
Binding bg = new Binding {Source = MazeElements[Index(xIndex, yIndex)]};
Rectangle nextRect = new Rectangle();
nextRect.Height = MazeGridSize;
nextRect.Width = MazeGridSize;
nextRect.Fill = Brushes.White;
nextRect.Stroke = Brushes.Gray;
nextRect.StrokeThickness = 2;
nextRect.SetBinding(Shape.FillProperty, bg);
temp.Children.Add(nextRect);
Canvas.SetLeft(nextRect, xIndex * MazeGridSize);
Canvas.SetTop(nextRect, yIndex * MazeGridSize);
Where is my mistake? I don't understand how to use the Binding from the C# side.
First, you are not providing us with enough information. We cannot see the full nature of the type MazeElement. But I assume that the BackgroundColor is a Property on the MazeElement class
Second, the binding has a Source, an object in your case nextElement, and the a PropertyPath, in your case the BackgroundColor. Therefore your binding object should be:
Binding bg = new Binding {Source = nextElement, Path = new PropertyPath("BackgroundColor") };
In your case the source property is of the same type as the target dependency property, so the above will do what you want. If it was not the case, you can set a converter on the binding - Look that up, if you need that in another situation

Binding on a nested property without using XAML

How to make a binding on a nested target property, like Shape.Stroke.Color in WPF without using XAML ?
For a simple property I'm using a code looking like this :
var binding = new Binding("mySourceProperty");
binding.Source = mySourceObject;
myTargetObject.SetBinding(myTargetProperty, binding);
Where myTargetProperty can be, for example, Shape.StrokeProperty.
But now, how can I do the same thing on the ColorProperty of the Stroke of a Shape ?
Provided that the Shape's Stroke property holds a SolidColorBrush, you can use the static BindingOperations.SetBinding method:
var shape = new Path(); // or whatever
var binding = new Binding { Source = Colors.Red }; // or whatever
BindingOperations.SetBinding(shape.Stroke, SolidColorBrush.ColorProperty, binding);

C# WPF binding back and forth

I am new to binding in Wpf and I am probably overlooking something or going about it the wrong way.
Map of my bindings
I have a class called Navigator(Controller) which bridges the View(UI) and Service(DataProvider). The only thing that the view and the service exchange is the DataModel(CurrentData) that the Navigator holds.
public class Navigator : INotifyPropertyChanged
Singleton class using Static I() and Instance() to get instance
Binding myBinding = new Binding();
myBinding.Source = Navigator.I;
myBinding.Path = new PropertyPath("CurrentView");
myBinding.Mode = BindingMode.OneWay;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(MasterWindow.ViewFrame, Frame.ContentProperty, myBinding);
Binding myBinding2 = new Binding();
myBinding2.Source = Navigator.I;
myBinding2.Path = new PropertyPath("DataModel");
myBinding2.Mode = BindingMode.TwoWay;
myBinding2.BindsDirectlyToSource = true;
myBinding2.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(CurrentView, Page.DataContextProperty, myBinding2);
Binding myBinding11 = new Binding();
myBinding11.Source = Navigator.I;
myBinding11.Path = new PropertyPath("DataModel");
myBinding11.Mode = BindingMode.TwoWay;
myBinding11.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(ActiveService, UIService.ServiceModelProperty, myBinding11);
Binding myBinding10 = new Binding();
myBinding10.Source = Navigator.I.CurrentView;
myBinding10.Path = new PropertyPath("Title");
myBinding10.Mode = BindingMode.OneWay;
myBinding10.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
myBinding10.BindsDirectlyToSource = true;
BindingOperations.SetBinding(MasterWindow, Window.TitleProperty, myBinding10);
My code behind bindings in the Navigator class
The title in the View(Page) is bound to the hosts, MainWindow Title.
Where it breaks is when the CurrentView is changed, the Title is stuck
to the first view set.
One view displays Title depending on the datatype it is working with
<Page.Resources>
<conv:DataTypeToString x:Key="ToString"/>
</Page.Resources>
<Page.Title>
<Binding Path="DataType" Mode="OneWay" ConverterParameter="plural" Converter="{StaticResource ToString}"/>
</Page.Title>
Where it breaks is when the CurrentView is changed, the Title is stuck
to the first view set.
I am doing something wrong with Binding but can't find out what?
When you set the source of the binding like this:
myBinding10.Source = Navigator.I.CurrentView;
then the source for the binding is not "up to date" with whatever is held under the CurrentView property at any time, but rather the source is set to the value of that property at the moment when this line is executed. So even though the property value might change later on, the source will remain the same, and the title won't change. In order for the binding framework to also "track" changes to the CurrentView property (apart from the Title property) you should place it as part of the path, and not the source. So this should fix your problem:
myBinding10.Source = Navigator.I;
myBinding10.Path = new PropertyPath("CurrentView.Title");
Now whenever CurrentView changes or it's current value's Title changes, it will be reflected in your UI.

How to programmatically bind Visibility on a button to INotifyPropertyChanged boolean property in C#?

Here's what I got to so far:
System.Windows.Data.Binding binding = new System.Windows.Data.Binding("MyProperty");
binding.Mode = System.Windows.Data.BindingMode.TwoWay;
binding.Converter = new System.Windows.Controls.BooleanToVisibilityConverter();
binding.Source = mySourceObject;
this.SetBinding(this.myButton.Visibility, binding);
but visibility is not a dependency property, so how can I do this?
You should be able to do
Button.VisibilityProperty
instead of
this.myButton.Visibility
http://msdn.microsoft.com/en-us/library/system.windows.uielement.visibilityproperty.aspx
You need to pass in the static Visibility dependency property itself (which Button inherits from UIElement), not the value of the property on the button instance, e.g.:
myButton.SetBinding(UIElement.VisibilityProperty, binding);

Categories

Resources