In a WPF based project I would like to bind the content of a TextBox to two sliders in such a way that first slider would start selection (from left or right based on a checkbox) from the
n-th character (n being slider value) to the m-th character (based on second slider value).
In essence I would like to specify selection range based on slider values.
How can I achieve it?
Ideally, you would bind TextBox.SelectionStart and TextBox.SelectionLength to values from the slider. (Probably via a converter that implements IMultiValueConverer)
Unfortunately, you can't, because you can only bind Dependency Properties, and SelectionStart and SelectionLength are not dependency properties.
To solve this problem you would have to handle the OnValueChanged event on the sliders, then update the SelectionStart and SelectionLength properties via code in the event handler.
Disappointing answer - I bet you were hoping for some slick XAML code :-)
Related
I have a button that I want to disabled in some cases. To determinate that, I use the selection of many controls in the view. For simplify in this question, two comboBox.
So the IsEnabled depends on the combiation of the information in this two controls, I need to evaluate the new state when change one of them. How can I do that? I know that I need a multi value converter to determinate if the button is enabled or not, but I don't know how to execute the converter when the selection in one of the combobox is changed.
EDIT:
When I have said before Multi value converter I wanted to say multi binding.
Perhaps I have not been very clear. I want the following:
1.- In the beginning the button is disabled and the two comboBox have not any item selected.
2.- When I selected an item in one of the comboBox, I need to execute the multi binding that is used to set the IsEnabled property of the button.
And repeat the process when I selected a new item in any of the comboBox.
The problem that I have is that I don't know how to say to the button that when I change the selection in any of the comboBox, the button need to execute the multi binding to determinate the value of the IsEnabled property.
but I don't know how to execute the converter when the selection in one of the combobox is changed.
As long as the values you're binding to are either DependencyProperty values or part of a class that (properly) implements INotifyPropertyChanged, this will happen automatically. You shouldn't need to do anything to update the values.
Just make sure the bound values notify as if they were used directly, and WPF will handle this when using an IMultiValueConverter just fine.
Hi I have an image of a '-' to the left of my slider, and an image of a '+' to the right.
How do I bind these images such that if they are clicked on they will increase or decrease the slider by my value of SMALLCHANGE?
Thanks.
Mind that the easiest way is to just name your Slider, handle the Image.MouseLeftButtonUp in the UserControl and change its value in code behind. If you doesn't plan to reuse this behavior, there is no need to do any further.
To make this behavior reusable, there are several approaches. Initially I'd suggest using a EventTrigger in the image with a CallMethodAction bound to the Slider, but the Slider does not has methods like LargeIncrement, SmallIncrement, LargeDecrement, SmallDecremenet. You could subclass it and implement these methods yourself, though.
Another option is to create an AttachedProperty like "IncrementSlider" to the Image (or any FrameworkElement), which would receive a Slider instance through a ElementName binding. In the PropertyChangedCallback (see this) of the PropertyMetadata, you could subscribe to the MouseLeftButtonUp event of the control and modify the Slider. You could create other attached properties as well, like "DecrementSlider", "SmallIncrementSlider", as well.
A better solution than the AttachedProperty in this case would be an AttachedBehavior. You can create a behavior to a FrameworkElement (or just Image) with a Slider property and other control properties, like "IncrementOrDecrement" and "SmallOrLarge". In the behavior's OnAttached method you would subscribe to the MouseLeftButtonUp of the control to update the Slider (if there is one available). You could then attach the behavior to the Images, bind the Slider property of the behavior to the desired Slider, and configure it to each of them, to increment or decrement in large or small steps.
I have an ItemsControl with a list of radio buttons - in this case the ItemTemplate will contain a binded radio button.
The problem is that in some cases I need to replace the radio button with a check box without using any C# code. Is this possible? I thought at using a ValueConverter (C# code) but I don't know for sure if will work.
You could put both a radio button and a checkbox in the template, then use a binding expression in the "Visible" property that ensures only one of them is visible based on the source object being bound. If your logic to decide visibility is more complex than just a boolean on the source object, use a valueconverter in the binding expression.
I'm working in Silverlight, trying to figure out how to set a grid cell font color based on the contents of the cell.
I have an ObservableCollection bound to a DataGrid, and my items implement INotifyPropertyChanged so the grid updates as I change the values; it's all working perfectly, including letting me sort items and keep the sorting while I update the underlying items.
I know I can use the LoadingRow event to change colors, but the only way I can get the event to fire is by changing the grids datasource, in which case my sorting goes out the window.
So, what I really want is a way to either
loop the rows in the datagrid,
find the cell I need, and change
it's color or
implement a custom
column that I can use to dynamically
set the color.
The problem is how to actually do either of those things :).
You should use databinding for this.
Bind your cell font color to the content of the cell
Create a converter IValueConverter that converts the value to a color depending from your needs
See here for a good example
http://weblogs.asp.net/joewrobel/archive/2009/01/25/conditional-formatting-in-the-silverlight-datagrid.aspx
I am trying to bind to a combobox text with the IsEditable property set to true. I have a property in my viewmodel which is bound to the text.
I want to validate on the text being typed in the text of the combobox, and restrict some values that the user is typing in. So some will be allowed, and some not, and these need to set the combobox back to its old value.
I do this in the view model and I have tried setting my text property in my view model explicity to the old value or just ignoring the change and raising that the property has been changed, but for the life of me it will not refresh the text back to the old value.
Is this because the combobox is editable, and it has the text caret and focus somewhere in the text of the combobox.
Basically, I want it to refresh back to the previous text when I restrict some typing in the combobox during in editing. Anyone have any ideas to reset the text back to its old value through the ViewModel. Thanks in advance!
Thanks for your replies. But I could never get it to work instead, I made my own UserControl which comprises a textbox overlayed over a combobox, and manipulate those two controls to meet my needs. A long way to go to solve a simple problem, but it works in the end.
Is the viewmodel property you are binding to created as a DependencyProperty? This is probably the problem you are facing Two-way binding in WPF
If you don't want to create a Dependency property then you need to implement INotifyProperty changed and manually force the update in the Property changed event.
I think this is because of a 'bug' in WPF not refreshing the UI if you change the value of a property in the setter. You can workaround it by implementing an IdentityConverter that force the UI to refresh as per this arcticle.