【问题标题】:silverlight 2 binding data to transforms?silverlight 2 将数据绑定到转换?
【发布时间】:2008-11-13 06:15:51
【问题描述】:

我正致力于在 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>

【问题讨论】:

  • 这是我在 Silverlight 3 中期待的事情之一。

标签: c# silverlight binding silverlight-2.0


【解决方案1】:

问题好像是Rule #1 from this post:

数据绑定的目标必须是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();
    }

}

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

【讨论】:

    猜你喜欢
    • 2010-10-20
    • 2012-01-14
    • 2011-07-30
    • 2011-04-13
    • 2012-03-10
    • 2014-10-17
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    相关资源
    最近更新 更多