ivalueconverter

Databind a nullable type in XAML\Windows 8 store app

依然范特西╮ 提交于 2019-12-10 22:51:47
问题 I have a TextBox in XAML which I'm trying to databind to a nullable int. This is the code for my textbox and linked converter: <TextBox x:Name="textArea" InputScope="Number" Text="{Binding Area, Mode=TwoWay, Converter={StaticResource NullableValueConverter}}" /> public class NullableValueConverter:IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { return value; } public object ConvertBack(object value, Type targetType, object parameter,

WPF string to double converter

三世轮回 提交于 2019-12-10 20:21:59
问题 Could someone give me some hints as to what I could be doing wrong? so I have a textblock in xaml <TextBlock> <TextBlock.Text> <Binding Source="signal_graph" Path="GraphPenWidth" Mode="TwoWay" Converter="{StaticResource string_to_double_converter}" /> </TextBlock.Text> </TextBlock> which attached to signal_graph's GraphPenWidth property (of type double). The converter is declared as a resource in the app's resources and looks like this: public class StringToDoubleValueConverter :

What is the best practice for WPF ValueConverter error handling?

ⅰ亾dé卋堺 提交于 2019-12-10 17:46:23
问题 Two function should be implemented, and I am wondering what type of validation I need to do on inputs and how to manage errors. Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert End Function Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As

Getting a reference to the ViewModel from an IValueConverter

不羁的心 提交于 2019-12-10 07:48:09
问题 Is there a clean and/or an accepted standard way of referring back to the ViewModel from an IValueConverter, or does that break the MVVM pattern? Basically, I want to convert bound data in the UI based on other properties of the ViewModel. I guess this is the same question as how do you refer back to the Window/Page from an IValueConverter since a reference to the control is not passed to the Convert/ConvertBack methods. 回答1: I would suggest adding a new property to the ViewModel that

How to pass a static value to IValueConverter in XAML

谁说胖子不能爱 提交于 2019-12-10 02:19:10
问题 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 {

IValueConverter with Bound Dependency Properties

我的未来我决定 提交于 2019-12-09 16:16:05
问题 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

IValueConverter with MarkupExtension

孤街浪徒 提交于 2019-12-08 22:00:36
问题 Recently I read about an IValueConverter which also inherits from MarkupExtension . It was something like: internal class BoolToVisibilityConverter : MarkupExtension, IValueConverter { private static BoolToVisibilityConverter converter; public BoolToVisibilityConverter() { } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is bool) { if ((bool)value) { return Visibility.Visible; } } return Visibility.Collapsed; } public object ConvertBack

Binding Double to TextBox

佐手、 提交于 2019-12-08 16:21:08
问题 I have often used TextBox to bind to Integers without much problem. However if I try to bind a TextBox to a Double it doesn't work. When I type 5,85 ( , being my cultures decimalSeperator) I pass 585.0 to the double value. How is it being converted and what solution could I use to fix this? Would a ValueConverter be the best solution? 回答1: You could try adding this to your application's constructor: FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new

Getting a reference to the ViewModel from an IValueConverter

只愿长相守 提交于 2019-12-05 16:12:30
Is there a clean and/or an accepted standard way of referring back to the ViewModel from an IValueConverter, or does that break the MVVM pattern? Basically, I want to convert bound data in the UI based on other properties of the ViewModel. I guess this is the same question as how do you refer back to the Window/Page from an IValueConverter since a reference to the control is not passed to the Convert/ConvertBack methods. I would suggest adding a new property to the ViewModel that concatenates or combines the other properties in the ViewModel. This eliminates the need for an IValueConverter all

Defining a Property in a IValueConverter class

不羁的心 提交于 2019-12-05 08:24:37
I need to define a DependencyProperty in a converter class because I need this data to make the conversion and this data is in another object, not the one I'm binding to. My converter class is the following: public class LEGOMaterialConverter : DependencyObject, IValueConverter { public DependencyProperty MaterialsListProperty = DependencyProperty.Register("MaterialsList", typeof(Dictionary<int, LEGOMaterial>), typeof(LEGOMaterialConverter)); public Dictionary<int, LEGOMaterial> MaterialsList { get { return (Dictionary<int, LEGOMaterial>)GetValue(MaterialsListProperty); } set { SetValue