I write code for multilanguage using IValueConverter.
Text will be change dynamically and so many texts has. So I cannot be make using Resource file.
I decided to make using IValueConverter. And First time it displayed well.
But change language runtime, it will not work I expected.
IValueConverter will return a value by current language type.
And language type is change in runtime by user.
This is my simplified code:
internal class MessageConverter : IValueConverter, INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string? valueOrigin = parameter == null ? ":" : parameter.ToString();
string[] values = valueOrigin == null ? new string[2] : valueOrigin.Split(':');
if (values.Length == 0)
{
values = new string[] { string.Empty, string.Empty };
}
else if (values.Length == 1 && false == values[0].StartsWith("#"))
{
values = new string[] { string.Empty, values[0] };
}
if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
{
return values[1];
}
MessageHelper.GetInstance().LanguageChanged += MessageConverter_langchanged;
return MessageHelper.GetMessage(values[0]);
}
private void MessageConverter_langchanged(object? sender, EventArgs e)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("")); // ???
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class MessageHelper
{
private static object lockObject = new object();
private static MessageHelper? instance = null;
private Dictionary<string, string> ko = new Dictionary<string, string>();
private Dictionary<string, string> en = new Dictionary<string, string>();
private Dictionary<string, string> jp = new Dictionary<string, string>();
private MessageHelper()
{
// smaple
ko.Add("#MSG_0001", "시작");
en.Add("#MSG_0001", "Start");
}
public static MessageHelper GetInstance()
{
lock (lockObject)
{
if (instance == null)
instance = new MessageHelper();
return instance;
}
}
private string language = "KO";
public static string GetLanguage(string language)
{
return language;
}
public static void SetLanguage(string language)
{
MessageHelper.GetInstance().language = language;
GetInstance().LanguageChanged?.Invoke(GetInstance(), EventArgs.Empty);
}
public event EventHandler? LanguageChanged;
public static string GetMessage(string messageCode)
{
var dic = MessageHelper.GetInstance().language == "en" ? MessageHelper.GetInstance().en : MessageHelper.GetInstance().ko;
if (dic.ContainsKey(messageCode))
return dic[messageCode];
return messageCode;
}
}
<Button Style="{StaticResource BoldButtonFontStyle}"
Content="{Binding Converter={StaticResource MessageConverter}, ConverterParameter=#MSG_0001:시작하기, UpdateSourceTrigger=PropertyChanged}"
Command="{Binding CommonCommand}"
CommandParameter="SaleStart" />
<Button Style="{StaticResource BoldButtonFontStyle}"
Content="한국어"
Command="{Binding CommonCommand}"
CommandParameter="Language_Korean" />
<Button Style="{StaticResource BoldButtonFontStyle}"
Content="English"
Command="{Binding CommonCommand}"
CommandParameter="Language_English" />
private void CommonCommandProc(string param)
{
if (param == "Language_Korean")
{
MessageHelper.SetLanguage("ko");
}
else if (param == "Language_English")
{
MessageHelper.SetLanguage("en");
}
}
I code using inherit 'MarkupExtension'.
It works fine.
Thank you every one.
Related
I have a ComboBox listing an Enum.
enum StatusEnum {
Open = 1, Closed = 2, InProgress = 3
}
<ComboBox ItemsSource="{Binding StatusList}"
SelectedItem="{Binding SelectedStatus}" />
I want to display localized names for the enum values in English
Open
Closed
In Progress
but also in German (and other languages in the future)
Offen
Geschlossen
In Arbeit
In my ViewModel using
public IEnumerable<StatusEnum> StatusList
{
get
{
return Enum.GetValues(typeof(StatusEnum)).Cast<StatusEnum>();
}
}
only gets me the names of the enum in the code and not the translated ones.
I have general localization in place and can access them using i.e.
Resources.Strings.InProgress
which gets me the translation for the current language.
How can I bind the localization automatically?
It's an example of the simple Enum to translated string converter.
public sealed class EnumToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{ return null; }
return Resources.ResourceManager.GetString(value.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string str = (string)value;
foreach (object enumValue in Enum.GetValues(targetType))
{
if (str == Resources.ResourceManager.GetString(enumValue.ToString()))
{ return enumValue; }
}
throw new ArgumentException(null, "value");
}
}
Also you need a MarkupExtension which will provide values:
public sealed class EnumerateExtension : MarkupExtension
{
public Type Type { get; set; }
public EnumerateExtension(Type type)
{
this.Type = type;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
string[] names = Enum.GetNames(Type);
string[] values = new string[names.Length];
for (int i = 0; i < names.Length; i++)
{ values[i] = Resources.ResourceManager.GetString(names[i]); }
return values;
}
}
Usage:
<ComboBox ItemsSource="{local:Enumerate {x:Type local:StatusEnum}}"
SelectedItem="{Binding SelectedStatus, Converter={StaticResource EnumToStringConverter}}" />
EDIT: You can make a more complex value converter and markup extension. The EnumToStringConverter can use DescriptionAttribute's to get the translated strings. And the EnumerateExtension can use TypeConverter.GetStandardValues() and a converter. This allows to get standard values of the specified type (not only Enums) and convert them to strings or something another depending on the converter.
Example:
<ComboBox ItemsSource="{local:Enumerate {x:Type sg:CultureInfo}, Converter={StaticResource CultureToNameConverter}}"
SelectedItem="{Binding SelectedCulture, Converter={StaticResource CultureToNameConverter}}" />
EDIT: The more complex solution described above is published on GitHub now.
You can do using a Attribute for the enum and writing an extension method for the enum. Refer the below code.
<ComboBox Width="200" Height="25" ItemsSource="{Binding ComboSource}"
DisplayMemberPath="Value"
SelectedValuePath="Key"/>
public class MainViewModel
{
public List<KeyValuePair<Status, string>> ComboSource { get; set; }
public MainViewModel()
{
ComboSource = new List<KeyValuePair<Status, string>>();
Status st=Status.Open;
ComboSource = re.GetValuesForComboBox<Status>();
}
}
public enum Status
{
[Description("Open")]
Open,
[Description("Closed")]
Closed,
[Description("InProgress")]
InProgress
}
public static class ExtensionMethods
{
public static List<KeyValuePair<T, string>> GetValuesForComboBox<T>(this Enum theEnum)
{
List<KeyValuePair<T, string>> _comboBoxItemSource = null;
if (_comboBoxItemSource == null)
{
_comboBoxItemSource = new List<KeyValuePair<T, string>>();
foreach (T level in Enum.GetValues(typeof(T)))
{
string Description = string.Empty;
FieldInfo fieldInfo = level.GetType().GetField(level.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
Description = GetDataFromResourceFile(attributes.FirstOrDefault().Description);
}
KeyValuePair<T, string> TypeKeyValue = new KeyValuePair<T, string>(level, Description);
_comboBoxItemSource.Add(TypeKeyValue);
}
}
return _comboBoxItemSource;
}
public static string GetDataFromResourceFile(string key)
{
//Do you logic to get from resource file based on key for a language.
}
}
I have already posted a similar thing in SO Is it possible to databind to a Enum, and show user-friendly values?
You can't, out of the box.
But you can create an ObservableList<KeyValuePair<StatusEnum, string>> property and fill it with your enum/localized text and then bind it to your ComboBox.
As for the string itself:
var localizedText = (string)Application.Current.FindResource("YourEnumStringName");
Getting the Enum string representation with Enum.GetName/Enum.GetNames methods.
I have an ObservableCollection<dynamic> class and XAML refuses to bind to the properties on the contained objects.
I know I read somewhere that XAML supports dynamic and DyanmicObject so I'm mightily confused on why this is not working. Other questions, such as this one, were spectacularly un-helpful:
Can i bind against a DynamicObject in WinRT / Windows 8 Store Apps
I get this error at runtime (and in the designer at design time when hovering over my {Bindings):
Error: BindingExpression path error: 'DisplayName' property not found on 'PremiseMetro.Light, PremiseMetro, ... BindingExpression: Path='DisplayName' DataItem='PremiseMetro.Light, PremiseMetro, ... target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')
Please help!
Thanks.
A test ObservableObject class:
class Light : DynamicObject, INotifyPropertyChanged {
private readonly Dictionary<string, object> _properties = new Dictionary<string, object>();
public event PropertyChangedEventHandler PropertyChanged;
public override bool TryGetMember(GetMemberBinder binder, out object result) {
string name = binder.Name;
result = null;
// If the property name is found in a dictionary,
// set the result parameter to the property value and return true.
// Otherwise, return false.
object prop;
if (_properties.TryGetValue(name, out prop)) {
result = prop;
return true;
}
return false;
}
// If you try to set a value of a property that is
// not defined in the class, this method is called.
public override bool TrySetMember(SetMemberBinder binder, object value) {
string name = binder.Name;
_properties[name] = value;
if (CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess)
OnPropertyChanged(name);
else
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
CoreDispatcherPriority.Normal, () => OnPropertyChanged(name));
// You can always add a value to a dictionary,
// so this method always returns true.
return true;
}
public object GetMember(string propName) {
var binder = Binder.GetMember(CSharpBinderFlags.None,
propName, GetType(),
new List<CSharpArgumentInfo> {
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
});
var callsite = CallSite<Func<CallSite, object, object>>.Create(binder);
return callsite.Target(callsite, this);
}
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">Name of property</param>
/// <param name="val">New value</param>
/// <param name="fromServer">If true, will not try to update server.</param>
public void SetMember(String propertyName, object val) {
var binder = Binder.SetMember(CSharpBinderFlags.None,
propertyName, GetType(),
new List<CSharpArgumentInfo> {
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
});
var callsite = CallSite<Func<CallSite, object, object, object>>.Create(binder);
callsite.Target(callsite, this, val);
}
protected virtual void OnPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
A test in my MainViewMOdel constructor:
Light light = new Light();
((dynamic) light).DisplayName = "Test Light";
((dynamic) light).Brightness= "27%";
((dynamic) light).PowerState= false;
Lights = new ObservableCollection<dynamic> {
light
};
My test XAML:
<Grid Margin="10" Width="1000" VerticalAlignment="Stretch">
<ListBox x:Name="GDOList" ItemsSource="{Binding Path=Lights}" >
<ListBox.ItemTemplate>
<DataTemplate >
<Grid Margin="6">
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=DisplayName}" Margin="5" />
<TextBlock Text="{Binding Path=PowerState}" Margin="5" />
<TextBlock Text="{Binding Path=Brightness}" Margin="5" />
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Page>
Can you try changing your ViewModel to:
dynamic light = new ExpandoObject();
light.DisplayName = "Test Light";
light.Brightness = "27%";
light.PowerState = false;
var objs = new ObservableCollection<dynamic> { light };
and see if that works in your lib correctly?
Short Answer: no, binding to an dynamic property on an instance of DynamicObject in UWP is not supported.
However, there do exist some way to do similar thing, with a ICustomPropertyProvider.
Assume your class looks something like this:
public class SomeClass : DynamicObject, INotifyPropertyChanged {
private string _StaticStringProperty;
public string StaticStringProperty { get => _StaticStringProperty; set => SetField(ref _StaticStringProperty, value); }
private Dictionary<string, object> _DynamicProperties = new Dictionary<string, object>();
public override IEnumerable<string> GetDynamicMemberNames()
{
yield return "DynamicStringProperty";
yield return "DynamicIntegerProperty";
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = _DynamicProperties.GetValueOrDefault(binder.Name, null);
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_DynamicProperties[binder.Name] = value;
RaisePropertyChanged(binder.Name);
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
protected bool SetField<T>(ref T target, T value, [CallerMemberName]string caller = null)
{
if (EqualityComparer<T>.Default.Equals(target, value))
return false;
target = value;
RaisePropertyChanged(caller);
return true;
}
}
now let it implement ICustomPropertyProvider:
public class SomeClass : DynamicObject, ICustomPropertyProvider, INotifyPropertyChanged {
...
public Type Type => GetType();
public string GetStringRepresentation() => ToString();
public ICustomProperty GetCustomProperty(string name)
{
switch (name)
{
// the caveat is that you have to provide all the static properties, too...
case nameof(StaticStringProperty):
return new DynamicCustomProperty<SomeClass, string>()
{
Name = name,
Getter = (target) => target.StaticStringProperty,
Setter = (target, value) => target.StaticStringProperty = value,
};
case "DynamicStringProperty":
return new DynamicCustomProperty<SomeClass, string>()
{
Name = name,
Getter = (target) => target.DynamicStringProperty,
Setter = (target, value) => target.DynamicStringProperty = value,
};
case "DynamicIntegerProperty":
return new DynamicCustomProperty<SomeClass, int>()
{
Name = name,
Getter = (target) => target.DynamicIntegerProperty,
Setter = (target, value) => target.DynamicIntegerProperty = value,
};
}
}
throw new NotImplementedException();
}
...
}
and be able to provide the DynamicCustomProperty:
public class DynamicCustomProperty<TOwner, TValue> : ICustomProperty
{
public Func<dynamic, TValue> Getter { get; set; }
public Action<dynamic, TValue> Setter { get; set; }
public Func<dynamic, object, TValue> IndexGetter { get; set; }
public Action<dynamic, object, TValue> IndexSetter { get; set; }
public object GetValue(object target) => Getter.Invoke(target);
public void SetValue(object target, object value) => Setter.Invoke(target, (TValue)value);
public object GetIndexedValue(object target, object index) => IndexGetter.Invoke(target, index);
public void SetIndexedValue(object target, object value, object index) => IndexSetter.Invoke(target, index, (TValue)value);
public bool CanRead => Getter != null || IndexGetter != null;
public bool CanWrite => Setter != null || IndexSetter != null;
public string Name { get; set; }
public Type Type => typeof(TValue);
}
finally we're able to bind them in XAML:
<TextBox Header="Static String" Text="{Binding StaticStringProperty, Mode=TwoWay}"/>
<TextBox Header="Dynamic String" Text="{Binding DynamicStringProperty, Mode=TwoWay}"/>
<TextBox Header="Dynamic Integer" Text="{Binding DynamicIntegerProperty, Mode=TwoWay}"/>
I have a WPF Converter which is slow (computations, online fetching, etc.). How can I convert asynchronously so that my UI doesn't freeze up? I found this, but the solution is to place the converter code in the property - http://social.msdn.microsoft.com/Forums/pl-PL/wpf/thread/50d288a2-eadc-4ed6-a9d3-6e249036cb71 - which I would rather not do.
Below is an example which demonstrates the issue. Here the dropdown will freeze until Sleep elapses.
namespace testAsync
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Data;
using System.Windows.Threading;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyNumbers = new Dictionary<string, int> { { "Uno", 1 }, { "Dos", 2 }, { "Tres", 3 } };
this.DataContext = this;
}
public Dictionary<string, int> MyNumbers
{
get { return (Dictionary<string, int>)GetValue(MyNumbersProperty); }
set { SetValue(MyNumbersProperty, value); }
}
public static readonly DependencyProperty MyNumbersProperty =
DependencyProperty.Register("MyNumbers", typeof(Dictionary<string, int>), typeof(MainWindow), new UIPropertyMetadata(null));
public string MyNumber
{
get { return (string)GetValue(MyNumberProperty); }
set { SetValue(MyNumberProperty, value); }
}
public static readonly DependencyProperty MyNumberProperty = DependencyProperty.Register(
"MyNumber", typeof(string), typeof(MainWindow), new UIPropertyMetadata("Uno"));
}
public class AsyncConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
object result = null;
if (values[0] is string && values[1] is IDictionary<string, int>)
{
DoAsync(
() =>
{
Thread.Sleep(2000); // Simulate long task
var number = (string)(values[0]);
var numbers = (IDictionary<string, int>)(values[1]);
result = numbers[number];
result = result.ToString();
});
}
return result;
}
private void DoAsync(Action action)
{
var frame = new DispatcherFrame();
new Thread((ThreadStart)(() =>
{
action();
frame.Continue = false;
})).Start();
Dispatcher.PushFrame(frame);
}
public object[] ConvertBack(object value, Type[] targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
and the XAML:
<Window x:Class="testAsync.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:testAsync"
Title="MainWindow" Height="200" Width="200">
<Window.Resources>
<local:AsyncConverter x:Key="asyncConverter"/>
</Window.Resources>
<DockPanel>
<ComboBox DockPanel.Dock="Top" SelectedItem="{Binding MyNumber, IsAsync=True}"
ItemsSource="{Binding MyNumbers.Keys, IsAsync=True}"/>
<TextBlock DataContext="{Binding IsAsync=True}"
FontSize="50" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource asyncConverter}">
<Binding Path="MyNumber" IsAsync="True"/>
<Binding Path="MyNumbers" IsAsync="True"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DockPanel>
</Window>
Note that all Bindings are now IsAsync="True", but this doesn't help.
The combobox will be stuck for 2000 ms.
I know you said you don't want to invoke the translation from the property setter, but I submit that it is a cleaner approach than the IValueConverter/IMultiValueConverter.
Ultimately, you want to set the value of the selected number from the combobox, and return from that immediately. You want to defer updating the displayed/translated value until the translation process is complete.
I think it is clearer to model the data such that the translated value is itself a property that just gets updated by an asynchronous process.
<ComboBox SelectedItem="{Binding SelectedNumber, Mode=OneWayToSource}"
ItemsSource="{Binding MyNumbers.Keys}"/>
<TextBlock Text="{Binding MyNumberValue}" />
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
MyNumbers = new Dictionary<string, int> { { "Uno", 1 }, { "Dos", 2 }, { "Tres", 3 } };
DataContext = this;
}
public IDictionary<string, int> MyNumbers { get; set; }
string _selectedNumber;
public string SelectedNumber
{
get { return _selectedNumber; }
set
{
_selectedNumber = value;
Notify("SelectedNumber");
UpdateMyNumberValue();
}
}
int _myNumberValue;
public int MyNumberValue
{
get { return _myNumberValue; }
set
{
_myNumberValue = value;
Notify("MyNumberValue");
}
}
void UpdateMyNumberValue()
{
var key = SelectedNumber;
if (key == null || !MyNumbers.ContainsKey(key)) return;
new Thread(() =>
{
Thread.Sleep(3000);
MyNumberValue = MyNumbers[key];
}).Start();
}
public event PropertyChangedEventHandler PropertyChanged;
void Notify(string property)
{
var handler = PropertyChanged;
if(handler != null) handler(this, new PropertyChangedEventArgs(property));
}
}
You could use a DispatcherFrame for this, here's an example converter:
public class AsyncConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
object result = null;
DoAsync(() =>
{
Thread.Sleep(2000); // Simulate long task
result = (int)value * 2; // Some sample conversion
});
return result;
}
private void DoAsync(Action action)
{
var frame = new DispatcherFrame();
new Thread((ThreadStart)(() =>
{
action();
frame.Continue = false;
})).Start();
Dispatcher.PushFrame(frame);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
It is not a good design to have heavy computations in a converter - especially if you are making functionality others should use as a good example.
I would rewrite and use MVVM with your ViewModel as converter on steroids where you can do all those things in a transparent way - easier to program, more understandable programflow, easier to understand code.
And then you could utilize Prioritybindings:
http://msdn.microsoft.com/en-us/library/system.windows.data.prioritybinding.aspx
For your original problem I would look at when the converter is called - if it is when the binding has returned its value, you probably can't get Async to do what it does. I suspect that wpf waits for the property to return and then calls the converter - in that case it might not be possible to get your converter to not freeze gui.
An approach you could take:
In your converter you should start fetching your data and return for example with backgroundworker - otherwise ui will freeze.
In the multibinding pass a reference to something so when your data arrives you can fire propertychanged
I suggest looking at the BackgroundWorker. It can perform the translation on a background thread and then raises a completed event on the UI thread.
See http://www.dotnetperls.com/backgroundworker
I have 2 PasswordBoxes. I need to check are passwords equal. I don't want to write this condition into [].xaml.cs code, but i want to mark PasswordBox in red when passwords aren't equal.
Should i write special ValidationRule, some code in ViewModel or something else? Can anyone help me? Now the validation is written in the [].xaml.cs but i want to avoid it.
Using:
<PasswordBox Name="tbPassword" />
<PasswordBox Name="tbPasswordConf" />
<PasswordValidator
Box1="{Binding ElementName=tbPassword}"
Box2="{Binding ElementName=tbPasswordConf}" />
Code (this code is not cover all cases):
public class PasswordValidator : FrameworkElement
{
static IDictionary<PasswordBox, Brush> _passwordBoxes = new Dictionary<PasswordBox, Brush>();
public static readonly DependencyProperty Box1Property = DependencyProperty.Register("Box1", typeof(PasswordBox), typeof(PasswordValidator), new PropertyMetadata(Box1Changed));
public static readonly DependencyProperty Box2Property = DependencyProperty.Register("Box2", typeof(PasswordBox), typeof(PasswordValidator), new PropertyMetadata(Box2Changed));
public PasswordBox Box1
{
get { return (PasswordBox)GetValue(Box1Property); }
set { SetValue(Box1Property, value); }
}
public PasswordBox Box2
{
get { return (PasswordBox)GetValue(Box2Property); }
set { SetValue(Box2Property, value); }
}
private static void Box1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
private static void Box2Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var pv = (PasswordValidator)d;
_passwordBoxes[pv.Box2] = pv.Box2.BorderBrush;
pv.Box2.LostFocus += (obj, evt) =>
{
if (pv.Box1.Password != pv.Box2.Password)
{
pv.Box2.BorderBrush = new SolidColorBrush(Colors.Red);
}
else
{
pv.Box2.BorderBrush = _passwordBoxes[pv.Box2];
}
};
}
}
Also, it's possible to define dependency property with style of error and setting it instead of BorderBrush. But i don't know how to use in this case the standard ErrorTemplate.
Create a readonly property in your viewModel and return a brush for the border of the PasswordConfirmBox and then bind the textbox to the property
EDIT after comments received I've not tested this and there might be minor errors but this way you can return a different type depending on the converterparameter
public bool PasswordValidation
{
get
{
if (textBox1.Text == textBox2.Text)
return true;
else
return
false;
}
}
Value Converters:
public class ValConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (parameter == "")
{
if ((value as bool?) ?? false)
return Visibility.Hidden;
else
return Visibility.Visible;
}
else if (parameter == "")
{
if ((value as bool?) ?? false)
return new SolidColorBrush(Colors.Black);
else
return new SolidColorBrush(Colors.Red);
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (parameter == "Visibility")
{
if ((System.Windows.Visibility)value == Visibility.Visible)
return false;
else
return true;
}
else if (parameter == "Brush")
{
if (((SolidColorBrush)value).Color == Colors.Black)
return true;
else
return false;
}
}
Xaml:
<Window.Resources>
<vis:ValConverter x:Key="valConverter"/>
</Window.Resources>
<TextBox Name="textBox1"/>
<TextBox Name="textBox2" BorderBrush="{Binding ConfirmBorder,Converter={StaticResource valConverter},ConverterParameter=Brush}" />
<TextBlock Text="Passwords Does not Match!" BorderBrush="{Binding ConfirmBorder,Converter={StaticResource valConverter},ConverterParameter=Visibility}"/>
I have a UserControl that uses a binding converter. I've made the converter an inner class of
public partial class MyPanel : UserControl
{
public class CornerRadiusConverter : IValueConverter
{
How do I reference the Converter class from the XAML? The following does not work:
<controls:MyPanel.CornerRadiusConverter x:Key="CornerRadiusConverter" />
It gives this error:
The tag
'LensPanel.CornerRadiusConverter' does
not exist in XML namespace
'clr-namespace:MyApp.Windows.Controls'
I was thinking about this problem again, and I came up with something similar to Dennis's solution : create a "proxy" converter class, with a Type property, which will create the instance of the actual converter and delegate the conversion to it.
public class Converter : IValueConverter
{
private Type _type = null;
public Type Type
{
get { return _type; }
set
{
if (value != _type)
{
if (value.GetInterface("IValueConverter") != null)
{
_type = value;
_converter = null;
}
else
{
throw new ArgumentException(
string.Format("Type {0} doesn't implement IValueConverter", value.FullName),
"value");
}
}
}
}
private IValueConverter _converter = null;
private void CreateConverter()
{
if (_converter == null)
{
if (_type != null)
{
_converter = Activator.CreateInstance(_type) as IValueConverter;
}
else
{
throw new InvalidOperationException("Converter type is not defined");
}
}
}
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
CreateConverter();
return _converter.Convert(value, targetType, parameter, culture);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
CreateConverter();
return _converter.ConvertBack(value, targetType, parameter, culture);
}
#endregion
}
You use it like that :
<Window.Resources>
<my:Converter x:Key="CornerRadiusConverter" Type="{x:Type controls:MyPanel+CornerRadiusConverter}"/>
</Window.Resources>
It could be possible. A few months ago I wrote a markup extension to create the converter for you inline. It keeps a dictionary of weak references so that you don't create multiple instances of the same converter. Handles creating converters with different arguments too.
In XAML:
<TextBox Text="{Binding Converter={NamespaceForMarkupExt:InlineConverter {x:Type NamespaceForConverter:ConverterType}}}"/>
C#:
[MarkupExtensionReturnType(typeof(IValueConverter))]
public class InlineConverterExtension : MarkupExtension
{
static Dictionary<string, WeakReference> s_WeakReferenceLookup;
Type m_ConverterType;
object[] m_Arguments;
static InlineConverterExtension()
{
s_WeakReferenceLookup = new Dictionary<string, WeakReference>();
}
public InlineConverterExtension()
{
}
public InlineConverterExtension(Type converterType)
{
m_ConverterType = converterType;
}
/// <summary>
/// The type of the converter to create
/// </summary>
/// <value>The type of the converter.</value>
public Type ConverterType
{
get { return m_ConverterType; }
set { m_ConverterType = value; }
}
/// <summary>
/// The optional arguments for the converter's constructor.
/// </summary>
/// <value>The argumments.</value>
public object[] Arguments
{
get { return m_Arguments; }
set { m_Arguments = value; }
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
IProvideValueTarget target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
PropertyInfo propertyInfo = target.TargetProperty as PropertyInfo;
if (!propertyInfo.PropertyType.IsAssignableFrom(typeof(IValueConverter)))
throw new NotSupportedException("Property '" + propertyInfo.Name + "' is not assignable from IValueConverter.");
System.Diagnostics.Debug.Assert(m_ConverterType != null, "ConverterType is has not been set, ConverterType{x:Type converterType}");
try
{
string key = m_ConverterType.ToString();
if (m_Arguments != null)
{
List<string> args = new List<string>();
foreach (object obj in m_Arguments)
args.Add(obj.ToString());
key = String.Concat(key, "_", String.Join("|", args.ToArray()));
}
WeakReference wr = null;
if (s_WeakReferenceLookup.TryGetValue(key, out wr))
{
if (wr.IsAlive)
return wr.Target;
else
s_WeakReferenceLookup.Remove(key);
}
object converter = (m_Arguments == null) ? Activator.CreateInstance(m_ConverterType) : Activator.CreateInstance(m_ConverterType, m_Arguments);
s_WeakReferenceLookup.Add(key, new WeakReference(converter));
return converter;
}
catch(MissingMethodException)
{
// constructor for the converter does not exist!
throw;
}
}
}
What I do is:
<Window.Resources>
<ResourceDictionary>
<Converters:BooleanNotConverter x:Key="BooleanNotConverter"/>
</ResourceDictionary>
</Window.Resources>
And then in the control
<CheckBox IsChecked="{Binding Path=BoolProperty, Converter={StaticResource BooleanNotConverter} />