ivalueconverter

Return a dynamic resource from a converter

喜欢而已 提交于 2019-12-05 07:06:33
I want to change the color of a WPF control depending on the state of a bool, in this case the state of a checkbox. This works fine as long as I'm working with StaticResources: My control <TextBox Name="WarnStatusBox" TextWrapping="Wrap" Style="{DynamicResource StatusTextBox}" Width="72" Height="50" Background="{Binding ElementName=WarnStatusSource, Path=IsChecked, Converter={StaticResource BoolToWarningConverter}, ConverterParameter={RelativeSource self}}">Status</TextBox> My converter: [ValueConversion(typeof(bool), typeof(Brush))] public class BoolToWarningConverter : IValueConverter {

Static Instance Base/Derived class

自古美人都是妖i 提交于 2019-12-05 06:07:06
I would like to write a static instance property in a base class and derive this, but I am facing some problems. Here is the code for the base class - I currently have: public abstract class ResourceInstance<T> { private static T _instance; public static T Instance { get { if (_instance != null) return _instance; var method = MethodBase.GetCurrentMethod(); var declaringType = method.DeclaringType; if (declaringType != null) { var name = declaringType.Name; _instance = (T)Application.Current.TryFindResource(name); } return _instance; } } } As you can see its primary use is for WPF Resources

How to pass a static value to IValueConverter in XAML

坚强是说给别人听的谎言 提交于 2019-12-05 01:50:00
I would like to use static texts fetched from a web service in my WP7 app. Each text has a Name (the indetifier) and a Content property. For example a text could look like this: Name = "M43"; Content = "This is the text to be shown"; I would then like to pass the Name (i.e. the identifier) of the text to an IValueConverter , which would then look up the the Name and return the text. I figured the converter to look something like this: public class StaticTextConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value !=

MarkupExtension.ProvideValue — Is the IServiceProvider actually used?

一世执手 提交于 2019-12-05 00:13:11
问题 I was going through some old code of mine and came across a hybrid IValueConverter / MarkupExtension class. It got me wondering if the IServiceProvider in the ProvideValue method was actually useful, and how it would be useful? I see that IServiceProvider only has one method: GetService , which must be cast to the proper service type. I also looked at the MarkupExtension.ProvideValue MSDN page and it lists different types of services. I guess, I'm just wondering if any of those services are

Why would putting a no-op Converter on a Binding change its behavior?

亡梦爱人 提交于 2019-12-04 11:27:17
问题 I'm in the midst of testing a user control I've built, and I'm encountering something that's inexplicable to me. The control's an extension of the ComboBox that handles values of a specific custom type. It has a dependency property of that custom type that is the target property of a Binding. I've got a trace statement in the setter, and I can see that the property is getting set. But it's not appearing in my user control. Now, ordinarily I'd say, okay, I've got a bug in my user control. I

Passing values to IValueConverter

末鹿安然 提交于 2019-12-04 03:31:46
问题 I have a ListView that has a Grid with two columns and many rows. Each row has a TextBlock in each column with each Text property binded to a value in ListView's ItemSource . I need to do some converting of the text in the second TextBlock depending on the value in the first TextBlock . How can I get the value of the first text box to the converter? Here is what I have so far: XAML: <UserControl.Resources> <local:ValueStringConverter x:Key="valueStringConverter" /> </UserControl.Resources>

IValueConverter with Bound Dependency Properties

风流意气都作罢 提交于 2019-12-04 03:04:39
I need to determine the StringFormat of some bound TextBlocks at runtime based on the unit system identified in the object to be bound. I Have a converter with a Dependency Property that I would like to Bind to. The Bound value is used in determining the conversion process. public class UnitConverter : DependencyObject, IValueConverter { public static readonly DependencyProperty IsMetricProperty = DependencyProperty.Register("IsMetric", typeof(bool), typeof(UnitConverter), new PropertyMetadata(true, ValueChanged)); private static void ValueChanged(DependencyObject source,

The point of ValueConversionAttribute class?

ぐ巨炮叔叔 提交于 2019-12-03 18:59:34
问题 What is the point of this attribute? After adding it I still need to make a cast on value object. [ValueConversion(sourceType: typeof(double), targetType: typeof(string))] public class SpeedConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var speed = (double)value; Is it only for code readability? Because when I change a binding's path to a String in xaml, Visual Studio doesn't give a warning about incorrect type and

Why would putting a no-op Converter on a Binding change its behavior?

安稳与你 提交于 2019-12-03 07:23:23
I'm in the midst of testing a user control I've built, and I'm encountering something that's inexplicable to me. The control's an extension of the ComboBox that handles values of a specific custom type. It has a dependency property of that custom type that is the target property of a Binding. I've got a trace statement in the setter, and I can see that the property is getting set. But it's not appearing in my user control. Now, ordinarily I'd say, okay, I've got a bug in my user control. I probably do, though I'm baffled about it. But this question isn't about finding the bug in my control.

Why isn't a property in my ViewModel updated when DataGrid changes?

半城伤御伤魂 提交于 2019-12-02 21:29:40
问题 I'm trying to create a UserControl, that will let me edit a Dictionary of type Dictionary<string,string> in a grid (just editing entries so far, not adding or deleting). Whenever I bind the DataGrid to a Dictionary it shows the grid as read only, so I decieded to create a value converter, that would convert it to an ObservableCollection<DictionaryEntry> where DictionaryEntry is just a class with two properties Key , and Value . This works for display the dictionary in the grid, but now when I