I'm trying to convert some XAML to C# code. Here's the xaml:
<ComboBox TextBlock.Foreground="{Binding DesiredForegroundBrush}"/>
I'd like to do the same thing in C# code, but I'm at a loss on how to access the TextBlock.
I tried the following:
ComboBoxInstance.TextBlock.SetBinding(TextBlock.ForegroundProperty, "DesiredForegroundBrush");
But the TextBlock is not accessible in C# code.
I also tried getting to the child of the combo box, but the GetChildrenCount returns 0:
var childrenCount = VisualTreeHelper.GetChildrenCount(ComboBoxInstance);
I did a few web searches but all I found was questions about how to bind combo boxes to TextBoxes.
I feel like there has to be an easy way to do this. Any help would be appreciated!
Update:
I have found this post:
How to I access an attached property in code behind?
But that only shows how to directly assign the property in the code behind, as opposed to set up binding on it.
use ComboBox.ForegroundProperty to bind the foreground Color. Why do you want the textbox ?
Every control derived from "Control" class will have Foreground property which is a dependency property and could be bound to any Brush value. To change the value of any control foreground we don't need to dig for the property.
<ComboBox Background="{Binding ForegroundColorBrush}"/>
and combobox foreground color will change according to binding.
Related
I was trying to get data from an observablecollection into my listbox, but instead of show the name of the item, I am getting Namespace.ItemClass (in my case, Retail_Items.RetailItem). I have attached an image. Where I might be wrong?
enter image description here
ListBoxItem exposes the value bound to ItemsSource by default.
And you probably bind a collection in the form of List <RetailItem>, so Namespace is printed out.
As with other people's answers, if the bound value is in the form of an object such as RetailItem, DisplayMemberPath can specify a specific property value as the output name.
This is because it dynamically creates Binding inside the ListBoxItem if DisplayMemberPath is present internally.
In fact, you can see the structure directly from the open source of the .Net Framework, which was published in Microsoft GitHub.
And I have distributed a sample source using ListBox/ListBoxItem's Style Template and MVVM structure to GITHub for you, so I hope it will help you.😀
👉 Sample Source
You should look into using a DataTemplate if you want to display multiple fields from your class or perhaps use the DisplayMemberPath property on the listbox to reference the relevant field.
Set the DisplayMember property of the listBox to "Name" (if you want the Name property to show).
e.g.-
listBox.DisplayMember = "Name";
I have a wpf user interface in which I use ComboBox to set a property to a value. The ItemsSource of the ComboBox has a Binding to a list of allowed values. I have made it so that this list is the list of allowed values but with the value currently selected removed from it. This way the list only contains values that one can actually change to. When the list is empty, I have a trigger that makes the combobox inactive.
This is all working well in terms of expected behaviour. However visually I have multiple red borders showing validation errors due to the fact the selectedvalue is no longer in the itemssource list upon update. There is no red border when I do not remove the currently SelectedValue from ItemsSource upon update.
Is there a way to get around this, maybe another Control I could use, or maybe somehow validate the SelectedValue from another list (that would contain the current value) compared to the one in ItemsSource ?
You can't actually select a value that is not in the ItemsSource. What you can do is to remove the red border that shows up when there is a validation error by setting the Validation.ErrorTemplate attached property to an empty ControlTemplate:
<ComboBox ...>
<Validation.ErrorTemplate>
<ControlTemplate/>
</Validation.ErrorTemplate>
</ComboBox>
I have a few TextBlocks that is stored within my ComboBox. What I would like to do is have each TextBlock have a different font color. I have seen many topics discussing how to do this, but I would like to know if it is possible to do it in code? The reason I ask is because the application connects to a server and gets items from the network which I then set in the TextBlock before pushing the item onto the ComboBox. I set the ForeGround and BackGround based o what the server tells me it should be, but the color always appears as black no matter what. Any advice would be greatly appreciated.
You want to create an ItemTemplate for the combobox and bind the FontColor property to a property in your model.
Check out this Q and A: Combobox ItemTemplate and foreground
Say I have a text box like this:
<TextBox Text="{Binding MyBoundProperty, Mode=TwoWay"/>
Is there an easy way to find and set "MyBoundProperty" in code behind?
I am working in a custom attached property for a TextBox (catching the OnTextBoxKeyUp event) and want to set the property directly in some scenarios (when a scan happens).
If it's two-way binding, changing the Text property of the TextBox should update the source.
I have a WPF application , and I would like to modify the background color of the UI based on the user's selection at run time .
Now , I want to create a button that changes the color each time its clicked , so this button must change a value from the XAML tags
Please , help me doing it , I need it badly
Thanks
To make a control red from code:
yourControl.Background = Brushes.Red;
Thomas has given one example of what you could do.
Another option would be to bind the background colour to some property of your data context (probably the ViewModel if you're using MVVM) and make the button click change that property - possibly indirectly via commanding.
They're just different approaches - directly setting the background colour is certainly simpler than going via binding, but it may be less easily testable.
Another option would be to bind the background colour to a property in your DataContext, and update only the property's value when you click the button. This way, you also get to keep your logic and display responsibilities separate.
To say it simply. There is no difference between XAML and C#. In the end it both produces same executable code.
To change property on GUI you should either name your control through x:Name property and then set your property in the backend code file. Or you can DataBind your property to some backing field, preferably by using MVVM pattern.
But you should first understant how WPF works (ESPECIALY DataBinding) before moving to more advanced topics like MVVM.