Binding enum type to textbox - wpf

I bind textbox.text value to enum type.
My enum looks like that
public enum Type
{
Active,
Selected,
ActiveAndSelected
}
What I wan't to acomplish is to show on textbox "Active Mode" instead of "Active" and so on. Is it possible to do that? It would be great if I could acomplish that in XAML - because all bindings I have in style file style.xaml
I was trying to use Description attributes but it seems that it's not enough

IMHO, using a converter is a better approach.
The first thing you should do is implement a simple attribute in order to add some metadata to your enum elements. Here's a basic example (without internationalization for simplicity):
public enum StepStatus {
[StringValue("Not done yet")]
NotDone,
[StringValue("In progress")]
InProgress,
[StringValue("Failed")]
Failed,
[StringValue("Succeeded")]
Succeeded
}
Next to that, you can write a utility class able to convert from an enum element to its corresponding StringValue representation using reflection. Search in Google for "String Enumerations in C# - CodeProject" and you'll find CodeProject's article about this (sorry, my low reputation won't let me add the link..)
Now you can implement a converter that simply delegates the conversion to the utility class:
[ValueConversion(typeof(StepStatus), typeof(String))]
public class StepStatusToStringConverter: IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture){
String retVal = String.Empty;
if (value != null && value is StepStatus) {
retVal = StringEnum.GetStringValue((StepStatus)value);
}
return retVal;
}
/// <summary>
/// ConvertBack value from binding back to source object. This isn't supported.
/// </summary>
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture) {
throw new Exception("Can't convert back");
}
}
Finally, you can use the converter in your XAML code:
<resourceWizardConverters:StepStatusToStringConverter x:Key="stepStatusToStringConverter" />
...
<TextBox Text="{Binding Path=ResourceCreationRequest.ResourceCreationResults.ResourceCreation, Converter={StaticResource stepStatusToStringConverter}}" ... />
Check the following page; it gives an example that supports internationalization, but basically the principle is the same..

You do not need a converter for this simple case. Use Stringformat in stead. The leading '{}' are an escape sequence to tell the parser that you do not mean to use them for another nested tag. If you add text before the bound text (indicated by '{0}'), you can remove them.
<Window x:Class="TextBoxBoundToEnumSpike.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBox Text="{Binding ModeEnum,StringFormat={}{0} Mode}"/>
<Button Click="Button_Click" Height=" 50">
Change to 'Selected'
</Button>
</StackPanel>
</Window>
using System.ComponentModel;
using System.Windows;
namespace TextBoxBoundToEnumSpike
{
public partial class MainWindow : Window,INotifyPropertyChanged
{
private ModeEnum m_modeEnum;
public MainWindow()
{
InitializeComponent();
DataContext = this;
ModeEnum = ModeEnum.ActiveAndSelected;
}
public ModeEnum ModeEnum
{
set
{
m_modeEnum = value;
if (PropertyChanged!=null)PropertyChanged(this,new PropertyChangedEventArgs("ModeEnum"));
}
get { return m_modeEnum; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void Button_Click(object sender, RoutedEventArgs e)
{
ModeEnum = ModeEnum.Selected;
}
}
public enum ModeEnum
{
Active,
Selected,
ActiveAndSelected
}
}

You can use a Converter to do this. Bind to the enum normally but add a Converter property to the binding. The converter is a class implementing IValueConverter, which will be called by WPF. There, you can add a suffix like "Mode" (or do whatever you like).

Related

In WPF how do I properly bind the style of a button within an ItemsControl template and use a converter?

In WPF, I am trying to have the button created as part of the ItemTemplate in a ItemsControl select the style using a property within the class. I thought I had it coded correctly, but when I run this, it creates the button "Test", but it never even runs the converter for the Style.
By the way, yes, I'm obviously aware that I haven't properly implemented the IValueConverter yet, but when I set breakpoints, it never even enters the converter.
Also, how do I properly keep the style of the button updated when the value of the property CurrentItemProperty changes?
MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:ButtonStyleConverter x:Key="ButtonStyleConverter"/>
</Window.Resources>
<Grid>
<ItemsControl x:Name="ButtonList">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="OptionButton">
<Button Content="{Binding DisplayName}" Style="{Binding CurrentItem, Converter={StaticResource ButtonStyleConverter}}"></Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
MainWindow.xaml.cs
namespace WpfApp1
{
public partial class MainWindow : Window
{
DependencyProperty CurrentItemProperty = DependencyProperty.Register("CurrentItemProperty", typeof(string), typeof(MainWindow));
public string CurrentItem
{
get
{
return (string)GetValue(CurrentItemProperty);
}
set
{
SetValue(CurrentItemProperty, value);
}
}
public MainWindow()
{
InitializeComponent();
ButtonList.ItemsSource = new OptionButton[]
{
new OptionButton() { DisplayName = "Test"}
};
CurrentItem = "Test";
}
public class OptionButton
{
public string DisplayName { get; set; }
}
}
public class ButtonStyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
when I set breakpoints, it never even enters the converter.
Because there's nothing to convert.
If you would run your code in the debugger and look at the debug output, you'd find that you're getting a binding error, where the binding engine fails to find the property CurrentItem on the source object. That's because the source object is a OptionButton instance, which of course does not have a CurrentItem property.
You have a number of options available to you:
Control the style some other way. There's not enough context in your question to know why you're trying to use a converter to return a style, but it's unusual enough an approach that I'll suggest this should be your first thing to try. There's probably a better way to control the styling of the button than using a converter this way.
Use a property that's on the OptionButton type. In your example, the CurrentItem and OptionButton.DisplayName have the same value, so taking the example literally you could just bind to the DisplayName property instead. Alternatively, maybe you could implement a property in OptionButton that delegates to the parent-level value you care about.
Set the binding source to the window, so that the CurrentItem property is visible. There are (at least) two variations on this theme: give the window a name attribute, and refer to it by name in the binding, or just use {RelativeSource AncestorType=Window} markup for the source of the binding.
The bottom line is that the binding doesn't work as expected because the current data context for the binding (which defines the default source for the binding) doesn't have the property you're binding to.

Can you replace characters in a textbox as you type?

We are looking for a way to make a textbox replace certain characters as a person types in it. Note, we are focusing on the control itself, not bindings, viewmodels, etc. For the sake of this question, assume there is a window with a textbox sitting in the middle of it and nothing else. No data, no viewmodel, no bindings, etc.
I've updated the question because it seems all the answers below keep focusing on bindings, dependency properties, coersion, etc. While I appreciate the suggestions, as I stated above, our control is not a bound control so they aren't applicable.
Now while I could explain the reasons for that, that would make this post about five times longer as it's actually a complex and advanced use-case, but that has nothing to do with the question itself, which is why I've simplified our scenario down to focus on the specific question we're trying to resolve, which is about a textbox control, or possibly a subclass of one replacing characters as you type.
Hope that makes more sense now.
The best way to accomplish this is using the TextChanged event:
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
var tb = (TextBox)sender;
using (tb.DeclareChangeBlock())
{
foreach (var c in e.Changes)
{
if (c.AddedLength == 0) continue;
tb.Select(c.Offset, c.AddedLength);
if (tb.SelectedText.Contains(' '))
{
tb.SelectedText = tb.SelectedText.Replace(' ', '_');
}
tb.Select(c.Offset + c.AddedLength, 0);
}
}
}
This has several advantages:
You don't go through the entire string every time, just the replaced part
It behaves well with the undo manager and with pasting text
You can easily encapsulate it into an attached property which can be applied to any text box
You can use an IValueConverter. It involves quite a bit of code, but it's the preferred way if you ever want to go the MVVM path, or just do things the WPF way.
First, create a class that implements IValueConverter
public class IllegalCharactersToUnderscoreConverter
: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var stringValue = value as String;
if(stringValue == null)
return value;
return RemoveIllegalCharacters(stringValue);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}
Import the namespace containing your ValueConverter
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
Create an instance of your converter in Window.Resources
<Window.Resources>
<local:IllegalCharactersToUnderscoreConverter x:Key="IllegalCharactersToUnderscore" />
</Window.Resources>
Use the converter in your binding expression. UpdateSourceTrigger=PropertyChanged is required for the text to be converted as you type.
<TextBox Text="{Binding MyText,
Converter={StaticResource IllegalCharactersToUnderscore},
UpdateSourceTrigger=PropertyChanged}"/>
You could subclass TextBox you can just override the Metadata of the TextBox Text property
public class FilteredTextBox : TextBox
{
public FilteredTextBox()
{
TextBox.TextProperty.OverrideMetadata(typeof(FilteredTextBox), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, CoerceMyTextValue, true, UpdateSourceTrigger.PropertyChanged));
}
private static object CoerceMyTextValue(DependencyObject d, object baseValue)
{
if (baseValue != null)
{
var userEnteredString = baseValue.ToString();
return userEnteredString.Replace(' ', '_');
}
return baseValue;
}
}
And you dont need to use bindings at all, this will just update the internal TextBox TextProperty as you type
<Window x:Class="WpfApplication13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="428" Width="738" Name="UI"
xmlns:my="clr-namespace:WpfApplication13" >
<StackPanel>
<my:FilteredTextBox />
</StackPanel>
</Window>
I tried to achieve this thing by handling this in my ViewModel property that is bind to TextBox itself like below and it works. In this example I replace '!' with underscore. Here i just put the replacement logic in property setter and if there is a change I replaced the text and Raised the propertychange async.
private string text;
public string Text
{
get { return text; }
set
{
text = value;
if (text.Contains("!"))
{
text = text.Replace('!', '_');
Dispatcher.BeginInvoke((Action) (() => RaisePropertyChange("Text")));
}
RaisePropertyChange("Text");
}
}
and the textbox binding is
<TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"/>

Binding to an expression

I have an onscreen numeric keypad to type a PIN. What I want to do is disable the buttons when four digits of PIN are entered. I can certainly do this with code pretty easily, but it seems to me to be the sort of thing that should be done with binding.
Something like:
<Button Style="Whatever" IsEnabled="{Binding ElementName=PinBox ???}"/>
It seems there isn't a way to do that (which to be honest seems rather primitive to me.) So I considered the alternative, which is a plain property on the underlying Window class. But I'm not sure how to bind to it. Do I need to specify the class itself as its own data context, or do I need to extract the PIN string into a View Model?
And subsequently, how do I get the plain property to update the GUI?
I suppose I could defined a view model class and have a dependency property called "ButtonsEnabled" but it seems kind of heavyweight for such a simple problem.
Let me know if I am missing something.
You can write a converter which return boolean depending on digits in TextBox
The XAML fo r button would be
<Button Content="Test" IsEnabled="{Binding ElementName=PinBox,Path=Text,Converter={StaticResource DigitsToBoolConverter}}" Grid.Row="1" Height="20" Width="100"></Button>
where PinBox is the textbox name used to enter pin.
The Converter function is
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.ToString().Length >= 4;
}
Another way using commands:
XAML:
<Button Content="2" Style="Whatever" Command={Binding MyCommand} CommandParamater="2"/>
ViewModel:
public ICommand MyCommand { get; private set; }
public string PinNumber { get; private set; }
public void Init()
{
MyCommand = new RelayCommand(
param => AddPinNumberDigit(param),
param => CanAddPin);
}
private void AddPinNumberDigit(string digit)
{
PinNumber += digit;
}
public bool CanAddPin {
get
{
return PinNumber.Length < 3;
}
}
Nope, your not missing anything, WPF out of the box bindings do not support expressions.
There has been some people implementing their own classes that add this type of functionality:
http://www.11011.net/wpf-binding-expressions
But really, this is what the ViewModel pattern is for. Use it, it's not heavyweight.
Create a converter that will return true or false based on PinBox.Text.Length.
Your xaml would then become:
<Button Style="Whatever" IsEnabled={Binding ElementName=PinBox, Converter={StaticResource yourConverter}}/>

WPF Equivalent of Winforms ListControl.Format Event? (Formatting List items with a delegate)

I'm finally making the switch from Winforms to WPF (3.5) , and I am trying to move this functionality over:
A common practice of mine is to control formatting of a list/combo box display in the Format event, by passing a delegate to the control's container. In the Format Event, the delegate formats the display text of the list item as I want it to appear (e.g. by combining properties of the displaying item).
Is there any equivalent way with the WPF Combo/List box to specify a delegate for formatting the appearance of List Item text at run time?
Thanks,
YS
FYI - Here what I was trying to get at, as described in my answer:
CodeBehind:
public partial class MainWindow : Window
{
private List<Foo> l = new List<Foo>();
//Formatting done by delegate, passed to converter.
MyConverter<Foo> cv = new MyConverter<Foo>(f=> "#" + f.ID + " = " + f.Name);
public MainWindow()
{
Resources.Add("myConverter", cv);
l.Add(new Foo(){ID=1, Name = "aaaa aaaa"});
l.Add(new Foo(){ID=2, Name = "bbbb bbbb "});
DataContext = l;
InitializeComponent();
}
}
public class Foo
{
public int ID { get; set; }
public string Name { get; set; }
}
public class MyConverter<T> : IValueConverter
{
private Func< T, string> _formatter { get; set; }
public MyConverter(Func<T, string> Formatter)
{
_formatter = Formatter;
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return _formatter((T)value);
}
}
And then in the xaml:
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource ResourceKey=myConverter}}"> </TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
If you are looking to format each item the same, look into ListBox.ItemContainerStyle. This will contain the template for each item. If you are looking into changing the styles for each item based on some logic, use the above along with ListBox.ItemContainerStyleSelector. See msdn doc http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemcontainerstyle.aspx and http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemcontainerstyleselector.aspx
For everyone breathlessly following this question...
I posted the question on the MS WPF forum, and from the answer there I think the best path for me is to create a custom IValueConverter where I can pass in a delegate to use in the Convert method, and set that as the Converter in the DataTemplate.

Can't bind ICommand in VM to button Command in xaml

I create a VM based on MVVM light toolkit.
In VM, there is a simple ICommand(RelayCommand)
private RelayCommand _myCommand = null;
public RelayCommand MyCommand
{
get
{
if (_myCommand == null) //set break point here for debug
{
_myCommand = new RelayCommand(() =>
{
try
{
//....
}
catch (Exception ex)
{
// notify user if there is any error
//....
}
}
, () => true);
}
return _myCommand;
}
}
then in xaml, just bind this Command property to a button like:
<Button Grid.Column="1" x:Name="Test" Content="Test" Margin="2,0,2,0" Command="{Binding Path=MyCommand}" />
Then run the app, and click on the button, there is no response at all. No error.
VM is working fine. The data has been loaded to a datagrid before I click on the Test button.
If debug the app and put break point, the point is never reached.
How to resolve this problem?
Add a setter to your MyCommand property.
As always, check the Output window for any data binding errors when the XAML is rendered.
Also, try adding a test value converter and putting a breakpoint in the convert method to see if data binding is even being executed on that command. If the breakpoint isn't hit, you know you have a problem in your XAML. If the breakpoint is hit, take a look at the value to see if the data context is correct.
<UserControl.Resources>
<ResourceDictionary>
<TestConverter x:Key="TestConverter" />
</ResourceDictionary>
<Button Grid.Column="1" x:Name="Test" Content="Test" Margin="2,0,2,0" Command="{Binding Path=MyCommand, Converter={StaticResource TestConverter}}" />
</UserControl>
Test value converter - very useful for debugging data binding issues.
public class TestConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Debug.WriteLine("TestConverter.Convert(value := {0}, targetType := {1}, parameter := {2}, culture := {3})",
value, targetType, parameter, culture);
return value; // put break point here to test data binding
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
Debug.WriteLine("TestConverter.ConvertBack(value := {0}, targetType := {1}, parameter := {2}, culture := {3})",
value, targetType, parameter, culture);
return value;
}
}
Works on my machine :)
Seriously, I made a simple project, created a ViewModel, pasted in your code, and it worked. I am guessing you are dealing with some other issue.
Here is my C# code.
Here is my XAML code.
Time to evangelize a bit
This ViewModel code reeks. You might consider using some sort MVVM framework or helpers. If you look at ViewModelSupport, for instance, you can write your ViewModel like this:
public class MyViewModel : ViewModelBase
{
public void Execute_MyCommand()
{
// Your execution code here
}
}
Then, you avoid all that messy plumbing. Just think about it :)
the code looks fine. so you just have to check the output window for databinding errors. maybe you did not set the datacontext of the view correct. btw you should add your breakpoint in the try-catch of the command.
1) Make sure you're returning true from the relay command's CanExecute delegate. (I see you are doing this but good to double check).
2) Is the button inside a ListBox, DataGrid or DataForm?
For a ListBox or DataGrid:
If so you need to modify your binding expression to refer to the VM DataContext as opposed to the databound item. See this answer.
For a DataForm :
More tricky, but look at this question.

Resources