开发者

Problem with binding in converter in silverlight custom control

开发者 https://www.devze.com 2022-12-19 19:44 出处:网络
i got some xaml here and what i m trying to do it\'s simply bind a property call Property (not the real name) on the width of a rectangle and to convert the value of this property with the converter n

i got some xaml here and what i m trying to do it's simply bind a property call Property (not the real name) on the width of a rectangle and to convert the value of this property with the converter name Conv and it's working perfectly with {TemplateBinding Property} or DataContext={TemplateBinding Property} or with a relative source (like in the code sample).

My problem is that the converterParameter should also be a binding property, but i m not able to bind any property in the converterParameter. So the 30 in the sample should be something like {Binding Path=SecondProperty}. If anyone go开发者_运维技巧t that problem or maybe if anyone got some other way to bind stuff in custom control thanks a lot ;)

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:controls="clr-namespace:RatingControl">
  <Style TargetType="controls:Ctr">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="controls:Ctr">
          <Grid>
            <Grid.Resources>
              <controls:Converter x:Name="Conv" />
            </Grid.Resources>
            <Rectangle x:Name="rect" Width="{Binding Path=Property, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource Conv}, ConverterParameter=30}" Height="20" />


It doesn't look like that's possible: http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converterparameter(VS.95).aspx


You can add a property to the Converter class and bind to that.


You can't bind to a property of the Binding object, since it isn't a DependencyProperty in fact Binding isn't a DependencyObject. This is understandable can you imagine the complexity of managing dependency trees and the possiblity of recursive or circular bindings in bindings.

However you could use a Specialised converter for the task:-

public class MySpecialConverter: IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Ctr obj = (Ctr)value;
        var val = obj.Property;
        var param = obj.SecondProperty;
        // Do your intended code with val and param here.
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException("This converter only works for one way binding");
    }
}

now your Xaml looks like:-

<Rectangle x:Name="rect" Height="20"
  Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource Conv}" />


It's a really good solution but it's not working bcs my first property must be bind (twoWay) because if i got any change on it the converter must convert again the value so i get the result back and show the real result.

0

精彩评论

暂无评论...
验证码 换一张
取 消