Winforms binding causing no radio button to be left selected - c#

Background
In this winforms app, there are two radio buttons that I'm attempting to bind to properties on a model class.
Code
Related properties on the Model:
private bool _bTotalRowsLinear;
private bool _bTotalRowsLog;
public bool bTotalRowsLinear
{
get { return _bTotalRowsLinear; }
set { _bTotalRowsLinear = value; }
}
public bool bTotalRowsLog
{
get { return _bTotalRowsLog; }
set { _bTotalRowsLog = value; }
}
Code to create the bindings:
rdbTotalRowsLinear.DataBindings.Add("Checked",
objModel,
"bTotalRowsLinear",
false,
DataSourceUpdateMode.OnPropertyChanged);
rdbTotalRowsLog.DataBindings.Add("Checked",
objModel,
"bTotalRowsLog",
false,
DataSourceUpdateMode.OnPropertyChanged);
Issue
The initial binding works correctly. However, when I attempt to select the nonselected radio option, I first end up with neither radio button selected, forcing the user to click their desired option twice.
Initial State:
After Clicking Once (error state):
Full code available on Github: https://github.com/nickheidke/datavelocityvisualizer

In your model, set the opposites, eg
set {
_bTotalRowsLinear = value;
_bTotalRowsLog = !bTotalRowsLinear;
}
...
set {
_bTotalRowsLog = value;
_bTotalRowsLinear = !bTotalRowsLog;
}

Related

How to Manipulate Output based on ListView Item Source Property

I am setting ListView ItemSource to a List<T> where T is my Model. I am Binding some of the Property of this List<T> to some Label in XAML. And Now based on a Property, I want to Set Label to some Text.
For Example, if (Property.IsCompleted == true), I might want to set a Label in my View Cell in the ListView to "Done" instead of "True".
I hope this summarizes the problem. I have tried other things and none worked.
This is the Item Appearing Method of My ListView:
private void bookingLV_ItemAppearing(object sender, ItemVisibilityEventArgs e)
{
BookingsModel convert = (BookingsModel)e.Item;
var select = convert.IsCompleted;
if(select == true)
{
IsDone = "Completed";
}
IsDone = "Pending";
}
And I have a Custom Property called IsDone:
public string IsDone { get; set; }
And This is how I am Binding IsDone in the View Cell of the ListView in Xaml
<Label Text="{Binding IsDone}"></Label>
I want to be able to set the Text Property of my Label to some text based on a property of my Model Object.
create a read only property in your model that returns a value based on another property
public string IsDone
{
get
{
if (select) return "Completed";
return "Pending";
}
}
if you are using INotifyPropertyChanged you will want to be sure that the setter of the "trigger" property fires PropertyChanged events for both
public bool selected {
get {
...
}
set {
...
PropertyChanged("selected");
PropertyChanged("IsDone");
}
}

Attached Property for Binding to WebBrowser not working

I have been looking for a way to get the HTML out of a WPF WebBrowser control. The two best options I have found are to bind a customer attached property to the property in the application or to build a new control from the WebBrowser control. Considering my level of knowledge and the fact that (as of now I really only need this one time) I chose the first. I even considered breaking MVVM style and using code-behind but I decided not to give up in the binding.
I found several examples on creating the attached property, I finally chose this one, from here Here:
namespace CumminsInvoiceTool.HelperClasses
{
public static class WebBrowserExtentions
{
public static readonly DependencyProperty DocumentProperty =
DependencyProperty.RegisterAttached("Document", typeof(string), typeof(WebBrowserExtentions), new UIPropertyMetadata(null, DocumentPropertyChanged));
public static string GetDocument(DependencyObject element)
{
return (string)element.GetValue(DocumentProperty);
}
public static void SetDocument(DependencyObject element, string value)
{
element.SetValue(DocumentProperty, value);
}
public static void DocumentPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
WebBrowser browser = target as WebBrowser;
if (browser != null)
{
string document = e.NewValue as string;
browser.NavigateToString(document);
}
}
}
}
I also added the following to the xaml for the WebBrowser control (I have tried both with and without the "Path=" in the xaml:
<WebBrowser local:WebBrowserExtentions.Document="{Binding Path=PageCode}" Source="https://www.cummins-distributors.com/"/>
My View has a tab control one tab has the WebBrowser control and another tab has a textbox. When I click the get code the viewModel runs a function to set property bound to the textbox to the string the attached property of the WebBrowser is bound to. Below is the code of my ViewModel.
namespace CumminsInvoiceTool.ViewModels
{
class ShellViewModel : Screen
{
private string _browserContent;
public string BrowserContent
{
get { return _browserContent; }
set {
_browserContent = value;
NotifyOfPropertyChange(() => BrowserContent);
}
}
private string _pageCode;
public string PageCode
{
get { return _pageCode; }
set {
_pageCode = value;
NotifyOfPropertyChange(() => PageCode);
}
}
public void StartProgressCommand()
{
}
public void GetContent()
{
if (!string.IsNullOrEmpty(PageCode))
{
BrowserContent = PageCode;
}
else
{
MessageBox.Show("There is no cintent to show", "No content Error", MessageBoxButton.OK);
}
}
}
}
The application compiles and runs but when I click "Get Code" I am getting the messagebox for "PageCode" is empty.
When I set a break point at the beginning of the function for the button, the PageCode string is showing "null".
Is this an issue because I am using Caliburn.Micro or am I missing something else?
------- EDIT for comments ----------
The button calls GetContent() in the "ShellViewModel" code above. I know the button is bound and working because the app is showing the custom messagebox I have set up to let me know when "pageCode" is null or empty.
The textbox looks like:
<TextBox x:Name="BrowserContent"/>

Edit my control from designer

I created control which contains a label and a textbox next to it:
The hierarchy look like that:
Panel
->Panel
->TextBox
->Label
I want to be able to custom the it on designer like change the textbox and label size and the text of the textbox.
Is there a easier way to do it without the needs to add property for each datamember of each control and calculate the sizes?
I have this code right now:
public override string Text
{
set { this.PhoneLabel.Text = value; }
get { return this.PhoneLabel.Text; }
}
public Size SizePhone
{
set { PhoneTextBox.Size = value; }
get { return PhoneTextBox.Size; }
}
public Size SizeLabel
{
set { PhoneLabel.Size = value; }
get { return PhoneLabel.Size; }
}
public Point LocationLabel
{
set { PhoneLabel.Location = value; }
get { return PhoneLabel.Location; }
}
I want to change the size with the mouse like I design a form
Thank you

Bind bool 'object not null' to Enabled property in control in Windows Forms

I have a bool variable, CanSave, and an object called Selected. Sometimes Selected is null so I need some textboxes to disable when this happens. This is my code:
private MyObject _selected;
public MyObject Selected
{
get { return _selected; }
set {
if (_selected != value)
{
_selected = value;
CanSave = Selected != null;
OnPropertyChanged("Selected");
}
}
}
private bool canSave;
public bool CanSave
{
get { return canSave; }
set { if (canSave != value)
{
canSave = value;
OnPropertyChanged("CanSave");
} }
}
I tried many things, I'm even binding to a Label and the label does respond to the property change.
txt_descripcion.DataBindings.Add(new Binding("Enabled", this, "CanSave")); //this doesn't work
label8.DataBindings.Add(new Binding("Text", this, "CanSave")); //this works
Any guide would be awesome, thanks in advance!
As #Igby Largeman said, I read the relevant code and thinking about how he actually made it work, and found somewhere in the code where I clear all bindings in the control. I haven't work in this code for weeks, so I forgot that happened, thanks Igby.

Howto Add Custom Control Property in property Dialog Box

I want to add property's to my custom control like above example property with descriptions!
I don't know hot to display that with GUI like above.
I want to know what attribute to use it.
private bool IsNum = true;
[PropertyTab("IsNumaric")]
[Browsable(true)]
[Description("TextBox only valid for numbers only"), Category("EmSoft")]
public bool IsNumaricTextBox
{
set
{
IsNum = value;
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (IsNum)
{
doStruf(e);
}
}
private void doStruf(KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+") && !char.IsControl(e.KeyChar))
e.Handled = true;
}
I want to display this as property tool box with Description
Like This in property box
IsNumaric True
The property requires a Getter in order to be displayed in the property grid:
private bool isNum = true;
[PropertyTab("IsNumaric")]
[Browsable(true)]
[Description("TextBox only valid for numbers only"), Category("EmSoft")]
public bool IsNumaricTextBox {
get { return isNum; }
set { isNum = value; }
}
It is quite easy to achieve, you just have to decorate it with an attribute like in the sample below:
[PropertyTab("IsNumaric")]
[DisplayName("NumericOrNot")]
[Category("NewCategory")]
public bool IsNumaricTextBox
{
set
{
IsNum = value;
}
}
and to make it work you need following using:
using System.ComponentModel
If you do not specify Category - property will show under Misc category (please note, that by default properties are being shown by names, not by categories). In this example the property is going to be shown under NewCategory and the name of the property is going to be NumericOrNot.

Categories

Resources