Question

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 : IValueConverter
  {
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
      double num;
      string strvalue = value as string;
      if (double.TryParse(strvalue, out num))
      {
        return num;
      }
      return DependencyProperty.UnsetValue;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
      return value.ToString();
    }
  }

What I thought would happen is that on startup the property value chosen by the default constructor would be propagated to the textblock, and then future textblock changes would update the graph when the textblock left focus. However, instead initial load does not update the textblock's text and changes to textblock's text have no effect on the graph's pen width value.

feel free to ask for further clarification.

Était-ce utile?

La solution

You do not need a converter for this, use the .ToString() method at the property.

public string GraphPenWidthValue { get { return this.GraphPenWidth.ToString(); } }

Anyway here is a Standart String Value Converter:

 [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();
        }
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top