1

我在这里得到了一些xaml,我试图做的只是在一个矩形的宽度上绑定一个属性调用Property(不是真实名称),并将这个属性的值转换为转换器名称Conv,它与{TemplateBinding Property} 或 DataContext={TemplateBinding Property} 或具有相对源(如代码示例中)。

我的问题是converterParameter 也应该是一个绑定属性,但我无法绑定converterParameter 中的任何属性。所以示例中的 30 应该类似于 {Binding Path=SecondProperty}。如果有人遇到这个问题,或者如果有人有其他方法来绑定自定义控件中的东西,非常感谢;)

<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" />
4

4 回答 4

0

这是一个非常好的解决方案,但它不起作用 bcs 我的第一个属性必须绑定(双向),因为如果我对其进行任何更改,转换器必须再次转换该值,以便我得到结果并显示真实结果。

于 2010-02-10T04:13:00.360 回答
0

看起来不可能: http: //msdn.microsoft.com/en-us/library/system.windows.data.binding.converterparameter (VS.95).aspx

于 2010-02-09T06:26:33.837 回答
0

您可以将属性添加到 Converter 类并绑定到该类。

于 2010-02-09T07:53:43.570 回答
0

您不能绑定到Binding对象的属性,因为它不是 aDependencyProperty事实上 Binding 不是DependencyObject. 这是可以理解的,您可以想象管理依赖树的复杂性以及绑定中递归或循环绑定的可能性。

但是,您可以使用专用转换器来完成任务:-

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");
    }
}

现在您的 Xaml 看起来像:-

<Rectangle x:Name="rect" Height="20"
  Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource Conv}" />
于 2010-02-09T09:26:36.810 回答