繁体   English   中英

silverlight 2绑定数据进行转换?

[英]silverlight 2 binding data to transforms?

我正在Silverlight 2中创建标签云,并尝试将List集合中的数据绑定到TextBlock上的Scale转换。 运行此程序时,出现AG_E_PARSER_BAD_PROPERTY_VALUE错误。 是否可以将数据绑定值绑定到Silverlight 2中的转换? 如果不能,我可以做些什么来达到FontSize = {Binding Weight * 18}的作用,以将标签的权重乘以基本字体大小? 我知道这行不通,但是计算DataTemplate中项目的属性值的最佳方法是什么?

<DataTemplate>
<TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextWrapping="Wrap" d:IsStaticText="False" Text="{Binding Path=Text}" Foreground="#FF1151A8" FontSize="18" UseLayoutRounding="False" Margin="4,4,4,4" RenderTransformOrigin="0.5,0.5">
<TextBlock.RenderTransform>
    <TransformGroup>
        <ScaleTransform ScaleX="{Binding Path=WeightPlusOne}" ScaleY="{Binding Path=WeightPlusOne}"/>
    </TransformGroup>
</TextBlock.RenderTransform>

问题似乎是此帖子的规则1

数据绑定的目标必须是FrameworkElement。

因此,由于ScaleTransform不是FrameworkElement,因此不支持绑定。 我试图绑定到SolidColorBrush进行测试,并得到与ScaleTransform相同的错误。

因此,为了解决这个问题,您可以创建一个控件,该控件公开标签数据类型的依赖项属性。 然后,发生一个属性更改事件,该事件将标记数据的属性绑定到控件中的属性(其中之一就是比例转换)。 这是我用来测试的代码。

项目控制:

<ItemsControl x:Name="items">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
       <local:TagControl TagData="{Binding}" />
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

标记控件xaml:

<UserControl x:Class="SilverlightTesting.TagControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    >
    <TextBlock x:Name="text" TextWrapping="Wrap" FontSize="18" Margin="4,4,4,4">
      <TextBlock.RenderTransform>
          <ScaleTransform x:Name="scaleTx" />
      </TextBlock.RenderTransform>
    </TextBlock>
</UserControl>

标签控制代码:

public partial class TagControl : UserControl
{
    public TagControl()
    {
        InitializeComponent();
    }

    public Tag TagData
    {
        get { return (Tag)GetValue(TagDataProperty); }
        set { SetValue(TagDataProperty, value); }
    }

    // Using a DependencyProperty as the backing store for TagData.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TagDataProperty =
        DependencyProperty.Register("TagData", typeof(Tag), typeof(TagControl), new PropertyMetadata(new PropertyChangedCallback(TagControl.OnTagDataPropertyChanged)));

    public static void OnTagDataPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var tc = obj as TagControl;
        if (tc != null) tc.UpdateTagData();
    }

    public void UpdateTagData()
    {
        text.Text = TagData.Title;
        scaleTx.ScaleX = scaleTx.ScaleY = TagData.Weight;
        this.InvalidateMeasure();
    }

}

仅仅设置一个属性似乎有点过分,但是我找不到更简单的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM