I want to set the control binding property "updatesource=Explicit" in cs file (dynamically) not in UI end. Please help me how can i do this?
it works :)
this.GetBindingExpression(SomeProperty).ParentBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
Are you creating the binding in code manually? If so you can just set it like any other property:
var binding = new Binding("BindingPath")
{
Source = myDataObject,
UpdateSourceTrigger = UpdateSourceTrigger.Explicit
}
textBlock.SetBinding(TextBlock.TextProperty, binding);
More information here.
I tested it and it works. :-) The code remains the same as Gimalay's.
BindingExpression bindingExpr = this.textBox1.GetBindingExpression(TextBox.TextProperty);
bindingExpr.ParentBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
Related
I am creating an AppBarButton in code behind file of XAML view.
Currently, I have AppBarButton defined in XAML as below:
<AppBarButton x:Name="SelectVisitAppBarButton" x:Uid="AppBar_TransferVisit" Margin="0,0,12,0" Icon="Bullets" Style="{StaticResource TransferAppBarButtonStyle}" IsEnabled="{Binding ScheduleViewVm.IsVisitSelectionEnable}" Click="SelectVisit_Click" />
I want to convert this XAML into C# code. Following is the code that I have till now.
AppBarButton SelectVisitAppBarButton = new AppBarButton();
SelectVisitAppBarButton.Margin = new Thickness(0, 0, 12, 0);
SymbolIcon bulletSymbol = new SymbolIcon();
bulletSymbol.Symbol = Symbol.Bullets;
SelectVisitAppBarButton.Icon = bulletSymbol;
SelectVisitAppBarButton.Style = App.Current.Resources["TransferAppBarButtonStyle"] as Style;
SelectVisitAppBarButton.Click += SelectVisit_Click;
Binding b = new Binding();
b.Source = _viewModel.ScheduleViewVm.IsVisitSelectionEnable;
SelectVisitAppBarButton.SetBinding(Control.IsEnabledProperty, b);
Appbar.Children.Add(SelectVisitAppBarButton);
The only thing I am looking for is to convert x:Uid="AppBar_TransferVisit" into its equivalent C# code. Any help on this is highly appreciated.
Thanks,
Naresh Ravlani.
It seems that you cannot actually set the x:Uid attribute programmatically in UWP:
How to set control x:Uid attribute programmatically for a metro app control?
This is because it's a XAML directive rather than a property:
Doing localization in visualstatemanager by changing x:uid?
You will have to set the corresponding properties of the AppBarButton directly, e.g.:
AppBarButton SelectVisitAppBarButton = new AppBarButton();
var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();
var text = resourceLoader.GetString("SomeResource");
AppBarButton SelectVisitAppBarButton = new AppBarButton();
SelectVisitAppBarButton.Content = text;
I can confirm that I have spoken to the UWP-XAML platform team about programatically accessing x:UID and it is not possible. This is a shortcoming, if you ask me. But it is what it is at this time. :)
There's a workaround, using XamlReader to load the button from xaml.
Button btn = (Button)XamlReader.Load("<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Uid='btnSubmit' Margin='5,0,0,0'/>");
btn.OtherProperty = xxxx
I need to programmatically create a binding for a checkbox that resides in a WPF form. Because the checkbox is in a user control that gets added to the form multiple times, I'm unsure how to do this. I've created a binding for a DevExpress RichEdit control which worked and then modified that code for the checkbox, but it didn't work.
My code to return the binding is as follows:
private Binding SetIsCorrectBinding(int row)
{
Binding binding = new Binding("DataModel.DetailList[" + row + "].IsCorrect")
{
Path = new PropertyPath("DataModel.DetailList[" + row + "].IsCorrect"),
Mode = BindingMode.TwoWay
};
return binding;
}
The code to implement the binding is as follows:
Binding cbBind = SetIsCorrectBinding(row);
detailRow.IsCorrect_cb.SetBinding(CheckBox.ContentProperty, cbBind);
No matter what I try, the IsCorrect variable is always false.
Any help with this would be greatly appreciated.
Please try the next:
var xBinding = new Binding();
//a real instance of the object where the source property is defined
//it have to be the same instance which is defined in DataModel.DetailList
xBinding.Source = sourceInstance;
xBinding.Path = new PropertyPath("The_source_property_name");
xBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
xBinding.Mode = BindingMode.TwoWay;
//Use this instead the .SetBinding( , ) where the checkbox is the object to binded to
BindingOperations.SetBinding(checkbox, CheckBox.ContentProperty, xBinding);
Please look at the next solution here, I think it can supply an additional information for you.
I will glad to help if you will have the problem with code.
regards,
6 years late, but I had a similar issue. You need to bind to the Checkbox.IsCheckedProperty
detailRow.IsCorrect_cb.SetBinding(CheckBox.IsCheckedProperty, cbBind);
was the fix you needed.
I have a textbox with xaml markup like this:
<TextBox x:Name="txtHN" Text="{Binding Path=AN}"/>
The above code works perfectly well. But when i change the data binding implementation from XAML to code-behind, It does not work anymore. The following code-behind does not work anymore:
Binding textHnBinding = new Binding();
textHnBinding.Path = new PropertyPath("AN");
txtHN.SetBinding(TextBox.TextProperty, textHnBinding);
I had set the textbox.datacontext to the same collectionviewsource but the code-behind version does not work anymore. I had really no idea what seems to be the culprit.
I use the following code for the CVS.source:
IEnumerable<decimal> ANListWard4 = (from s in context.IPDAN
where ward.Contains(s.CURRENTWARD)
select s.AN).Distinct().OrderBy(n => n);
List<IPDAN> Ward4AN = new List<IPDAN>();
foreach (decimal d in ANListWard4)
{
IPDAN ward4AN1 = new IPDAN();
ward4AN1.AN = d;
Ward4AN.Add(ward4AN1);
}
I set the CVS.Source to Ward4AN. There was no instance where the Ward4AN was null, or has no data.
Check whether the collectionViewSource is not null when you are doing the binding.If the object is null when you are adding the binding, the binding might not work.
Also check whether in loaded event it works or not.
Can you try :
Binding textHnBinding = new Binding("AN");
txtHN.SetBinding(TextBox.TextProperty, textHnBinding);
This is how I done all my binding so I think it should work.
EDIT :
Long time no uses binding so my apologies if I'm wrong again :
Binding textHnBinding = new Binding("AN");
FrameworkElementFactory textHN = new FrameworkElementFactory(typeof(TextBox));
txtHN.SetBinding(TextBox.TextProperty, textHnBinding);
Can you try instantiate your Control like this instead of in Xaml to check if it will work (as me) ? Thank you.
I have a UserControl (Map) that contains a Canvas control. I am dynamically adding a control (Gate) to this canvas from the code behind.
I want the Gate objects DataContext to be the "Gate" property of the Map's DataContext. This is being done in the code behind.
Binding dataContextBinding = new Binding();
dataContextBinding.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
dataContextBinding.Path = new PropertyPath("DataContext.SelectedLevelModule.Gate");
dataContextBinding.Mode = BindingMode.TwoWay;
dataContextBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(gate, DataContextProperty, dataContextBinding);
After this block of code runs, the gate.DateContext is null...
Any ways this can be done? Drawing a blank..
Thanks
Harold
You are setting the property path to DataContext.SelectedLevelModule.Gate. You are then assigning the binding to the DataContextProperty. I think what is happening is that the path is now gate.DataContext.DataContext.SelectedLevelModule.Gate.
Try removing DataContext from your PropertyPath and see if that fixes it. You are already assigning it to the DataContext, you should not have to specify it in the Path.
var dataContextBinding = new Binding();
dataContextBinding.Path = new PropertyPath("SelectedLevelModule.Gate");
BindingOperations.SetBinding(gate, DataContextProperty, dataContextBinding);
Could somebody please take some time to show me a quick example on how to bind a text box to the property of an object from c# code? (I've tried to do it on my own, but i can't seem to get it right.)
Thank you guys. I just spent an hour before i realized how stupid i am ( i was setting the wrong object as the biding source).
Thank you all for your help.
Binding b = new Binding();
b.Source = yourObject;
b.Path = new PropertyPath("YourProperty");
yourTextBox.SetBinding( TextBox.TextProperty, b );
There are a lot of other properties on binding you can set. The above does one-way binding, but you can change that by setting the Mode property.
Binding the TexeBox Text property with the "Name" property in the ViewModel
Binding binding = new Binding("Name");
binding.Source = ViewModel;
binding.Mode = BindingMode.TwoWay;
SomeTextBox.SetBinding(TextBox.TextProperty, binding);