C# UserControl with combobox - c#

I created a UserControl with TextBox, Image and Label
In the properties of the usercontrol everything works great except the ComboBox, I can't load the datasource so that the client chooses the option it wants to give to the TextBox.
For example, let's suppose that the combobox has 3 options so that the TextBox only accepts the data input selected by the client
"only letters - only numbers - letters and numbers"
private Image image;
private string data;
[Category("Custom UI")]
public Image Image
{
get { return image; }
set {
image = value;
pictureBox.Image = value;
}
}
[Category("Custom UI")]
public string Data
{
get { return data; }
set {
data = value;
label.Text = value;
}
}
In the following image you can see that everything is going well except for the combobox, its data load is null
I want to load the combox with the 3 options outlined above, I don't know if what I'm doing is correct, please help
private ComboBox textOptions;
[Category("Custom UI")]
public ComboBox TextOptions
{
get { return textOptions; }
set
{
textOptions = value;
}
}

Related

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"/>

Winforms binding causing no radio button to be left selected

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;
}

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

Accessing TextBox.Text on dynamically added UserControl

On load of a new Window I add a dynamic number of UserControls (SetInformation) to my window as below
public Window_NewWorkoutLine(int NoOfSets, int workoutLineId)
{
InitializeComponent();
currentWorkoutLineId = workoutLineId;
for (int x = 1; x <= NoOfSets; x++)
{
SetInformation setInformation = new SetInformation(x);
StackPanel_Main.Children.Add(setInformation);
}
}
Each of the UserControls contains 2 textboxes, what I need to do is grab the Text property from each textbox on each UserControl and use them in an insert query to a database. The data from each UserControl will be added to a seperate row in the database.
Any ideas guys?
Thanks in advance
I think you could enumerate all UIElement or "StackPanel_Main", and then cast them.
For example (with use of System.Linq) :
foreach (SetInformation setInformation in StackPanel_Main.Children.OfType<SetInformation>())
{
string txt1 = setInformation.Text1;
string txt2 = setInformation.Text2;
}
But I think this would be better to associate a "Value Object" to each instance of your UserControl to separate User Interface (UI) from Data. You could do that with Binding for example.
First declare your class which will contain your data with implementation of INotifyPropertyChanged for UI integration :
/// <summary>
/// This objet contains all useful data
/// </summary>
public class InformationValueObject : INotifyPropertyChanged
{
private string _text1;
private string _text2;
public string Text1
{
get { return _text1; }
set { _text1 = value; OnPropertyChanged("Text1"); }
}
public string Text2
{
get { return _text2; }
set { _text2 = value; OnPropertyChanged("Text2"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Then add Binding to your UserControl, as you like, for example you could just set the DataContext of your UserControl like bellow, and then Bind each property to each TextBox from DataContext :
public partial class Window_NewWorkoutLine : Window
{
private List<InformationValueObject> _valueObjects = new List<InformationValueObject>();
public Window_NewWorkoutLine(int NoOfSets, int workoutLineId)
{
InitializeComponent();
currentWorkoutLineId = workoutLineId;
for (int x = 1; x <= NoOfSets; x++)
{
SetInformation setInformation = new SetInformation(x);
InformationValueObject vo = new InformationValueObject();
setInformation.DataContext = _vo;
_valueObjects.Add(vo);
StackPanel_Main.Children.Add(setInformation);
}
}
}
It is then easy to retrieve values by reading private collection "_valueObjects". No more need to enumerate User Interface composition.
It is really important to separate data and display.
My example could also be improved a lot.
For example, a ListView is composed (by default) by a StackPanel inside a ScrollViewer, and you can set the "ItemsSource" property with _valueObjects collection, and customize Item template to use your custom UserControl.
Then you can improve again by using a MVVM model with the Binding of your collection onto your ListBox. You could also use an ObservableCollection<InformationValueObject> instead of a List<InformationValueObject> if you want to be able to add or remove items dynamically...
I know this is not possible to masterize everything, but I think it could be great to tend to these solutions by separating data and visualization.
Best regards,
Maybe try adding a public property to your control to get and set text to the text boxes:
public string Text
{
get
{
return txtTextBox.Text.ToString();
}
set
{
txtTextBox.Text = HttpUtility.HtmlDecode(value);
}
}
You can add accessors to the user controls:
public class SetInformation{
private TextBox box1;
private TextBox box2;
public string Box1Text{
get{
return box1.Text;
}
}
public string Box2Text{
get{
return box2.Text;
}
}
...
...
}

Accessing Text Box Values from Form to another class

I have a WPF Application which contains a class called RateView.xaml.cs and MainWindow.xaml.cs
The MainWindow.xaml.cs contains three textboxes of which values I want to pass into the RateView.xaml.cs. The content of these textboxes can be changed by the end user but regardless of that I always want whatever the value is of the textbox to be going into rateview.xaml.cs.
How can this be done?
I am a newbie to coding hence not sure, someone mentioned Get and Set statements, if so how can I do these?
Currently I access my textboxes like this in the MainWindow:
private float GetSomeNumber()
{
bool Number1 = false;
float parsedNumber1Value = 0.00F;
Number1 = float.TryParse(Number1_TextBox.Text, out parsedNumber1Value);
return parsedNumber1Value;
}
The GetSomeNumber() method is then passed to another seperate class to do some calculation with.
On intital load it works of the value from my method, but once someone changes the value rateview.xaml.cs doesn't recognise this change and always uses the values that were first loaded.
Thanks
Just a small example (This is winforms)
This is the mainwindow, where your textbox is:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
1
public string TextBox1Text
{
get { return textBox1.Text; }
set { textBox1.Text = value;
}
}
and this is a class where you want to interact with the textboxes:
public class Test
{
public Test(Form1 form)
{
//Set the text of the textbox in the form1
form.TextBox1Text = "Hello World";
}
}
To get and set the value of a textbox within another class/form you can do it with something like:
public string TextBox1Text
{ get { return textBox1.Text; }
set { textBox1.Text = value; } }

Categories

Resources