silverlight自定义控件中转换器的绑定问题

发布于 2024-08-20 13:21:57 字数 1037 浏览 5 评论 0原文

我这里有一些xaml,我想做的只是在矩形的宽度上绑定一个属性调用Property(不是真实名称),并使用转换器名称Conv转换该属性的值,它与{TemplateBinding完美配合Property} 或 DataContext={TemplateBinding Property} 或使用相对源(如代码示例中所示)。

我的问题是转换器参数也应该是一个绑定属性,但我无法绑定转换器参数中的任何属性。因此示例中的 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" />

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

末骤雨初歇 2024-08-27 13:21:57

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

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

乖乖公主 2024-08-27 13:21:57

您无法绑定到 Binding 对象的属性,因为它不是 DependencyProperty,事实上 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}" />

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}" />
黎歌 2024-08-27 13:21:57

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文