How can I change textbox style in a textbox style data trigger? - wpf

I have created a default style for my textbox controls, but I would like to be able to change the style at runtime.
I have a property that I want to bind to that informs the UI if the data is mandatory or not, and I want to be able to change the style of the textbox to another style that displays a red line under the textbox. I can't use this in the standard textbox style I've created because the property is different for each textbox i.e. IsEmailMandatory, isNameMandatory.
I've tried to bind a data trigger in the style of the textbox for each textbox I have, but I get a message telling me I can't change a style within a style. so how do I do this?

You don't need to change the whole style in the trigger, just the template.
Another way to achieve something similar would be to use attached properties. Assuming that the IsEmailMandatory, isNameMandatory properties are on the DataContext not the control itself, you could then bind the value you want to an attached property and then trigger off that in your default style.

Related

How to get an existing property from style

I'm developing a WPF project where I need to validate textboxes on code-behind and then change the border color of those textboxes to some unknown color defined by the user configuration; the problem is that to overwrite the MouseOver effect I would need to set the value from XAML directly via ControlTemplate or Style.
Is there a way to get the current value assigned to the TextBox from XAML? Maybe binding it to itself?
I will appreciate any help you can give.
When validating a TextBox, or any control for that matter, it's common to use an adorner to display the validation error (in your case, setting the border color). You can have a look at an example here.
Using this method, you don't actually change the TextBox's border, but create a new visual layer on top of the TextBox that draws the new border around it. This is pretty much the way validations should be done in WPF, and it also solves your problem of having to deal with changing the TextBox's border value back and forth.

ListBox Item style

I need to write a style for a listboxItem that will add a button on the right of the listboxitem when it is selected or mouse over. The button need to have a command binding as well .
This style need to be used in different places.
also the button won't have a border. but when the mouse is over the button , it will have a border.
How can i write this?
I tried to write a style, but i can't bind to the command of the button that is inside the style.
Code used: (sorry i wasn't able to paste my code normally here so it is here http://justpaste.it/1b9s)
It would be great to see some code but an option that should work is to create a DataTemplate. You can set it on ListBox ItemTemplate property and use it in the different locations where you may need it.

WPF Change a label's display based upon a bound textbox contents change

I have a bound textbox and I need to change its associated display label to bold upon the presence of any content. I dont want to use javascript if at all possible.
Thanks
Why would javascript be involved?
Anyway, if this is really wpf, bind the FontWeight property of your label to the textbox's text property, using custom converter that converts null/empty strings to Normal font weight, and non-null/non-empty to Bold.

SilverLight - Binding a command to button within control template

I have a datagrid. Within that datagrid, I have bunch of column headers styles. Inside the control template of this style, there is a button. I need to bind a command to that button.
Note that, there is also a TextBlock within the header style which I bind using element to element binding, as I will not have my viewModel in the header's dataContext.
If I use the same idea to bind the button command, it doesnt work. Am I missing something?
If this is unclear, please let me know and I will post sample code.
Thanks in advance!
Sample code always helps :-)
Remember that each data row in your grid is bound to an item with your datagrid datacontext. If your command does not exist in your item class (which it probably shouldn't) then it won't get bound correctly.
Can you trigger your command from the SelectedItem changed property of your datagrid, rather than an internal button? You can do this with the following code: http://thoughtjelly.blogspot.com/2009/12/silverlight-commands-data-grid-row.html

WPF "flying" combobox

is it possible to create a combobox in wpf from code, which I could position wherever I want on the window?
In my case, I basically want to create something like a Autocomplete for DataGrid rows,...
Thanks!
TextBox supports auto complete, and you can create your datagrid's template column and you can specify the template with textbox which can interact and give you autocompletion.
if you put it in a <Popup> you can then position the popup wherever you want--it is not "bound" by a parent container.

Resources