C# 字符串到双转换器
有人能给我一些提示,说明我可能做错了什么吗 所以我在xaml中有一个文本块C# 字符串到双转换器,c#,wpf,ivalueconverter,C#,Wpf,Ivalueconverter,有人能给我一些提示,说明我可能做错了什么吗 所以我在xaml中有一个文本块 <TextBlock> <TextBlock.Text> <Binding Source="signal_graph" Path="GraphPenWidth" Mode="TwoWay" Converter="{StaticResource string_to_double_converter}" /> </TextBlock.Text> </Tex
<TextBlock>
<TextBlock.Text>
<Binding Source="signal_graph" Path="GraphPenWidth" Mode="TwoWay" Converter="{StaticResource string_to_double_converter}" />
</TextBlock.Text>
</TextBlock>
我认为会发生的是,在启动时,默认构造函数选择的属性值将传播到textblock,然后未来的textblock更改将在textblock离开焦点时更新图形。但是,初始加载不会更新textblock的文本,对textblock文本的更改不会影响图形的笔宽值
请随时要求进一步澄清 您不需要转换器为此,请在属性处使用.ToString()方法
public string GraphPenWidthValue { get { return this.GraphPenWidth.ToString(); } }
无论如何,这里有一个标准的字符串值转换器:
[ValueConversion(typeof(object), typeof(string))]
public class StringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? null : value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
你用错了方法:你需要一个双串转换器,而不是一个双串转换器。
[ValueConversion(typeof(object), typeof(string))]
public class StringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? null : value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}