从ItemsControl中的代码中获取transform属性的绑定

时间:2015-11-29 21:15:15

标签: c# xaml windows-runtime winrt-xaml code-behind

Xaml如下:

<ItemsControl
    x:Class="PowersOf2.Windows10.Views.Controls.Board"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:PowersOf2.Windows10.Views.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Name="Root" ItemsSource="{Binding Fields, ElementName=Root}" Loaded="Root_Loaded"
>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid
                Width="{Binding FieldWidth, ElementName=Root}"
                Height="{Binding FieldHeight, ElementName=Root}"
                Loaded="Grid_Loaded" Background="White"
            >
                <Grid.RenderTransform>
                    <TranslateTransform X="{Binding X}" Y="{Binding Y}"/>
                </Grid.RenderTransform>
                <TextBlock Text="{Binding Text}" Foreground="Black"/>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Fields IEnumerable Field X,其坐标为YFieldWidth。它们由视图模型管理。 FieldHeightTranslateTransform.X是在后面的代码中计算的依赖项属性。

如何在后面的代码中获取嵌套依赖项属性的绑定对象,例如TranslateTransform.YItemsControl

UPDATE:

基于this question:Fredrik的答案按预期工作,直到你在xaml中处理单个嵌入对象并绑定到非嵌套属性,而不是嵌套属性。由于Grid包含我的using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using Windows.UI.Xaml; namespace Controls { public sealed partial class Board { public Board() { InitializeComponent(); } private void Root_Loaded(object sender, RoutedEventArgs e) { FieldWidth = 100.0; FieldHeight = 100.0; Fields = new Field[] { new Field { X = 100, Y = 100, Text = "one" }, new Field { X = 300, Y = 300, Text = "two" } }; } public double FieldWidth { get { return (double)GetValue(FieldWidthProperty); } set { SetValue(FieldWidthProperty, value); } } public static readonly DependencyProperty FieldWidthProperty = DependencyProperty.Register( "FieldWidth", typeof(double), typeof(Board), new PropertyMetadata(0.0) ); public double FieldHeight { get { return (double)GetValue(FieldHeightProperty); } set { SetValue(FieldHeightProperty, value); } } public static readonly DependencyProperty FieldHeightProperty = DependencyProperty.Register( "FieldHeight", typeof(double), typeof(Board), new PropertyMetadata(0.0) ); public IEnumerable<Field> Fields { get { return (ObservableCollection<Field>)GetValue(FieldsProperty); } set { SetValue(FieldsProperty, value); } } public static readonly DependencyProperty FieldsProperty = DependencyProperty.Register( "Fields", typeof(IEnumerable<Field>), typeof(Board), new PropertyMetadata(null) ); private void Grid_Loaded(object sender, RoutedEventArgs e) { // here I want to get binding of RenderTransform's properties } } public class Field : INotifyPropertyChanged { private int _x; public int X { get { return _x; } set { _x = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("X")); } } private int _y; public int Y { get { return _y; } set { _y = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Y")); } } private string _text; public string Text { get { return _text; } set { _text = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Text")); } } public event PropertyChangedEventHandler PropertyChanged; } }

,此问题更加复杂

下面的代码:

{{1}}

1 个答案:

答案 0 :(得分:3)

我希望我没有错误地解释这个问题,但你可以得到转换和这样的绑定项目。

private void Grid_Loaded(object sender, RoutedEventArgs e)
{
    var grid = (Grid)sender;
    //the actual transformation
    var render = (Transform)grid.GetValue(RenderTransformProperty);
    //the field the transformation is bound to
    var field = (Field)grid.DataContext;

    //for now this only works in WPF
    var binding = BindingOperations.GetBinding(render, TranslateTransform.XProperty);
}

对此进行了编辑,但它对winrt不起作用 BindingOperations.GetBinding方法仅在WPF中可用。

希望winrt尽快得到这个。