Databinding Enabled if false - c#

I have a problem with databinding in a winforms-application.
In the following code i have a databinding to the enabled-property of a textbox. The enabled-state depends on the value of a checkbox.
tbAmount.DataBindings.Add("Enabled", checkBox, "Checked",
false, DataSourceUpdateMode.OnPropertyChanged);
in this code the textbox is enabled if the checkbox is checked. but i need it inverted. i want the textbox to be enabled if the checkbox is unchecked. How can i achieve this?

This should do it.
Binding bind = new Binding("Enabled", checkBox, "Checked");
bind.Format +=
(sender, e) =>
e.Value = !((bool)e.Value); // invert the checked value
textBox.DataBindings.Add(bind);

I know this is a very old post, but I have looked for something similar many times over the years and was never really happy with what I ended up using. Mike Park's answer is great, not only because it works, but because of how simple it is.
All I did was take Mike's answer and turn it into a Control extension. Thanks Mike!
Depending on where and how you use it, you may need to add a reference to System.Windows.Forms and a using System.Windows.Forms statement.
/// <summary>
/// Creates a DataBinding that allows you to bind to the Unchecked state instead of the normal Checked state.
///
/// Sample usage: In this case, I am enabling a Button when the CheckBox is unchecked.
/// // Defaults to Control Enabled property.
/// // Always bound to the DataSource Checked property.
/// YourButton.DataBindings.Add(YourButton.UncheckedBinding(YourCheckBox));
///
/// var binding = YourButton.UncheckedBinding(YourCheckBox);
/// YourButton.DataBindings.Add(binding);
///
/// Adapted - from answer by Mike Park answered Oct 18 '12 at 19:11
/// From: Databinding Enabled if false
/// Link: https://stackoverflow.com/questions/12961533/databinding-enabled-if-false
/// </summary>
/// <typeparam name="T">Constrained to be a type that inherits from ButtonBase. This includes CheckBoxes and RadionButtons.</typeparam>
/// <param name="control">The control that will consume the DataBinding.</param>
/// <param name="DataSource">The control to which we are binding. We will always bind to the Checked property.</param>
/// <returns>DataBinding that is bound to the Unchecked state instead of the usual Checked state.</returns>
public static Binding UncheckedBinding<T>(this Control control, T DataSource) where T : ButtonBase
{
return UncheckedBinding(control, "Enabled", DataSource);
}
/// <summary>
/// Creates a DataBinding that allows you to bind to the Unchecked state instead of the normal Checked state.
///
/// Sample usage: In this case, I am enabling a Button when the CheckBox is unchecked.
/// // Always bound to the DataSource Checked property.
/// YourButton.DataBindings.Add(YourButton.UncheckedBinding("Enabled", YourCheckBox));
///
/// var binding = YourButton.UncheckedBinding(YourCheckBox);
/// YourButton.DataBindings.Add(binding);
///
/// Adapted - from answer by Mike Park answered Oct 18 '12 at 19:11
/// From: Databinding Enabled if false
/// Link: https://stackoverflow.com/questions/12961533/databinding-enabled-if-false
/// </summary>
/// <typeparam name="T">Constrained to be a type that inherits from ButtonBase. This includes CheckBoxes and RadionButtons.</typeparam>
/// <param name="control">The control that will consume the DataBinding.</param>
/// <param name="DataSource">The control to which we are binding. We will always bind to the Checked property.</param>
/// <param name="PropertyName">The name of the property that is being bound.</param>
/// <returns>DataBinding that is bound to the Unchecked state instead of the usual Checked state.</returns>
public static Binding UncheckedBinding<T>(this Control control, string PropertyName, T DataSource) where T : ButtonBase
{
var binding = new Binding(PropertyName, DataSource, "Checked");
binding.Format += (sender, e) => e.Value = !((bool)e.Value);
return binding;
}

Related

How can you store a Control in a class and reuse it in a binding?

I am trying to reuse NumberBoxes for a GridView because having the NumberBoxes embedded directly in the GridView data template causes undesirable behavior, while reusing them does not. The problem is that I keep getting exceptions. They say "No installed components were detected" on the following line (templateRoot.FindName("NumberBox") as GridViewItem).Content = item.NumberBox; in this context
/// <summary>
/// The callback for updating a container in the GridView named CardGridView.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void UpdateGridViewContainer(ListViewBase sender, ContainerContentChangingEventArgs args)
{
if (args.Phase == 1)
{
Grid templateRoot = args.ItemContainer.ContentTemplateRoot as Grid;
CardItem item = args.Item as CardItem;
(templateRoot.FindName("NumberBox") as GridViewItem).Content = item.NumberBox;
TypedEventHandler<NumberBox, NumberBoxValueChangedEventArgs> handler =
(box, args) =>
{
if (!double.IsNaN(args.NewValue))
{
_viewModel.ChangeCount(args, item);
}
};
item.SetHandler(handler);
}
}
The exception is thrown when the Page that contains the GridView is left and renavigated to. I have tried nulling out the NumberBoxes when the page is left, but that did not work. Well, it appeared to before the issue cropped up again.
This is the code that nulls out the NumberBoxes
/// <summary>
/// Creates new NumberBoxes for when this Page is loaded again.
/// </summary>
private void ResetNumberBoxes()
{
foreach (CardItem card in CardGridView.Items.Cast<CardItem>())
{
card.ResetNumberBox();
}
CardGridView.ItemsSource = null;
CardGridView.Items.Clear();
}
ResetNumberBox is just setting the NumberBox to null and assigning a new one.
The exception details
System.Runtime.InteropServices.COMException
HResult=0x800F1000
Message=No installed components were detected. (0x800F1000)
Source=WinRT.Runtime
StackTrace:
at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
at ABI.Microsoft.UI.Xaml.Controls.IContentControlMethods.set_Content(IObjectReference _obj, Object value)
An update. I have removed the GridViewItem control from the DataTemplate and tried doing the following with the same result
CardItem item = args.Item as CardItem;
Grid.SetColumn(item.NumberBox, 1);
item.NumberBox.HorizontalAlignment = HorizontalAlignment.Center;
item.NumberBox.VerticalAlignment = VerticalAlignment.Center;
(templateRoot.FindName("GridViewTemplate") as Grid).Children.Add(item.NumberBox);
I also examined a heap dump right before the line that throws, and there was only one instance of the Grid, and the NumberBox had a parent of null.

How to create DataTemplate with code? [MAUI]

How to create a DataTemplate with code?
DataTemplate does not have a VisualTree property like in WPF.
The docu doesn't help either.
There is an IDataTemplateController, but it does not control anything. <ignorable>weird MAUI times again</ignorable>.
Found it in source code: There is a constructor parameter for a function which creates the view.
/// <Docs>
/// <param name="loadTemplate">A custom content generator to be called </param>
/// <summary>Creates and initializes a new instance of the <see cref="T:Microsoft.Maui.Controls.DataTemplate" /> class.</summary>
/// <remarks>To be added.</remarks>
/// </Docs>
public DataTemplate(Func<object> loadTemplate);
new DataTemplate(() => {
var label = new Label();
label.SetBinding(Label.TextProperty, new Binding("."));
return label;
});
There is currently a bug in Binding and you have to specify ".", a fix is merged.

how to get inline linebreaks like \n for intelicense not paragraphs

I know i can use < para /> to create multiline function comments
/// <summary>
/// updates the car scrolling.<para />
/// there are also colors
/// <para />while clearall will remove all </summary>
/// <param name="status">optional to add a new car </param>
public void UpdateDrawing(int updatestatus = bgcar.color.none)
but that behaviour isnt like a linebreak \n it rather behaves like a double line brake ( \n\n ). Is there a way to have only a single line break.
So textblocks could be created for intellisence
Try this:
/// <summary>
/// <para>updates the car scrolling.</para>
/// <para>there are also colors</para>
/// <para>while clearall will remove all</para>
/// </summary>

How to disallow interaction on a view behind uipopovercontroller

I have a UIPopoverController that I am using and I have two buttons each displays a popup when clicked. However, I do not want the popup to be displayed at the same time - meaning I do not want the user to be able to press the one button and while the popup is displayed be able to press the other button. It seems like I have tried everything - disabling the user interaction on the buttons, hiding the view behind the pop up, using passthrough views for the pop and more. None of it works! The disabling of the user interaction seems to work for the most part but then stops disallowing the user to interact with the button and causes the application to crash...
popupView.PassthroughViews = new UIView[]{this.View.Superview, this.View, this.Gray}; //gray is another view that sits under the view that calls the popup
this.View.UserInteractionEnabled = false;
this.PositiveMeterBtn.UserInteractionEnabled = false;
this.View.Hidden = true;
My UIPopoverController is declared at the class level and I have even done code like this:
if(popupView != null)
return;
I still get multiple popups. I am using mono touch/xamarin - is this a bug with xamarin or an ios issue? Am I handling this in the correct manner?
I haven't worked with Xamarin before, but what's worked for me in native Objective-C is
[controller setModalInPopover:YES];
where controller is the view controller displayed within the popover.
From the UIViewController class reference:
#property(nonatomic, readwrite, getter=isModalInPopover) BOOL modalInPopover
The default value of this property is NO. Setting it to YES causes an owning popover controller to disallow interactions outside this view controller while it is displayed.
You can either make the popover modal but if it doesn't contain content that is meant to be modal, you shouldn't block the user.
Usually the better option is to make two helper methods and place them for instance in your app delegate. The methods take care that an existing popover is dismissed if another one is to be shown. This way you will have a maximum of on UIPopoverController and don't have to worry about dismissal.
/// <summary>
/// Shows a popover.
/// </summary>
/// <param name='controllerToShow'>the controller to show in the popover</param>
/// <param name='showFromRect'>the rectangle to present the popover from. Not used if showFromItem is specified.</param>
/// <param name='showInView'>the view the popover is hosted in</param>
/// <param name='showFromItem'>the bar button item the popover gets presented from.</param>
/// <param name='popoverContentSize'>the content size of the popover</param>
/// <param name='animated'>If set to <c>true</c>, animated the popover</param>
/// <param name='arrowDirection'>the allowed arrow directions</param>
/// <param name='onDismiss'>callback if the popover gets dismissed. Careful that the object that owns the callback doesn't outlive the popover controller to prevent uncollectable memory.</param>
public static void ShowPopover(UIViewController controllerToShow, RectangleF showFromRect, UIView showInView, UIBarButtonItem showFromItem, SizeF popoverContentSize, bool animated = true, UIPopoverArrowDirection arrowDirection = UIPopoverArrowDirection.Any, EventHandler onDismiss = null)
{
if(AppDelegateBase.popoverController != null)
{
AppDelegateBase.DismissPopover(false);
}
if(showFromItem == null && showFromRect.IsEmpty)
{
// Nothing to attach the popover to.
return;
}
popoverController = new UIPopoverController(controllerToShow);
if(!popoverContentSize.IsEmpty)
{
popoverController.SetPopoverContentSize(popoverContentSize, false);
}
if(onDismiss != null)
{
popoverController.DidDismiss += onDismiss;
}
// Send a notification that a popover will be presented.
NSNotificationCenter.DefaultCenter.PostNotificationName("WillPresentPopover", popoverController);
if(showFromItem != null)
{
popoverController.PresentFromBarButtonItem(showFromItem, arrowDirection, animated);
}
else
{
popoverController.PresentFromRect(showFromRect, showInView, arrowDirection, animated );
}
}
/// <summary>
/// Dismisses the popover presented using ShowPopover().
/// </summary>
/// <param name='animated'>If set to <c>true</c>, animates the dismissal</param>
public static void DismissPopover(bool animated = false)
{
if(popoverController != null)
{
popoverController.Dismiss(animated);
}
AppDelegateBase.popoverController = null;
}
private static UIPopoverController popoverController;
One thing you might try is using the method
-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
And in that method check if one of your popover view controller's is on screen.
if (popupView.view.window) {
return NO;
} else {
return YES;
}

How to localize DatePicker and TimePicker in WP8

I am attempting to localize the DatePicker and TimePicker associated with the toolkit on WP7, but I am unsure of how to access the Header and the application bar texts. I have not been able to find any links showing methods of accomplishing these tasks. Are there any useful links or does someone have the source of how this many be accomplished?
Easiest method to download Source and Samples of the latest version of the Toolkit (Nov 2011) which has by default localization for DatePicker and TimePicker.
Add the it as project reference to your solution.
If you have the Toolkit version prior to Nov 2011,
Again add it as Project Reference in your Solution
In side the project tookit in your solution. Add the necessary resx files. You can see there is a default Resources.resx file which has the English text for date pickers. Add the necessary resx files for other languages.
It's very simple: Parameter - Language. Xaml code:
<toolkit:DatePicker Language="ru-RU" Margin="-12, 0" Value="{Binding BirthDate, Mode=TwoWay}" />
Another alternative without modifying the XAML source is to modify the "HeaderTitle" TextBlock once the page loads.
/// <summary>
/// Called from app.xaml.cs if the user navigates to the DatePickerPage
/// </summary>
/// <param name="page">The page.</param>
public static void DatePickerHook(PhoneApplicationPage page)
{
// Somehow modify the text on the top of the page...
LoopThroughControls(page, (ui => {
var tb = ui as TextBlock;
if (tb != null && tb.Name == "HeaderTitle")
{
tb.Text = "<<Local Translation>>";
}
}));
}
/// <summary>
/// Applies an action to every element on a page
/// </summary>
/// <param name="parent">The parent.</param>
/// <param name="modifier">The modifier.</param>
private static void LoopThroughControls(UIElement parent, Action<UIElement> modifier)
{
int count = VisualTreeHelper.GetChildrenCount(parent);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
modifier(child);
LoopThroughControls(child, modifier);
}
}
return;
}
Here's the link to the blog post that describes the modifications to app.xaml.cs also: http://blog.dotnetframework.org/2015/11/09/localise-datepicker-in-wp8-silverlighttoolkit-using-hooks/

Categories

Resources