无法在Windows运行时组件库中的UserControl中创建依赖项属性

时间:2013-02-25 07:00:45

标签: c# data-binding windows-runtime winrt-xaml

我想在用户控件中创建数据可绑定属性。此用户控件包含“Windows运行时组件”项目。我使用下面的代码来创建属性。

public MyItem CurrentItem
{
    get { return (MyItem)GetValue(CurrentItemProperty); }
    set { SetValue(CurrentItemProperty, value); }
}

// Using a DependencyProperty as the backing store for CurrentItem. 
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty CurrentItemProperty =
    DependencyProperty.Register("CurrentItem", typeof(MyItem), typeof(CollapseUserControl), new PropertyMetadata(null));

当我编译项目时,我得到以下错误。

Type 'HierachyLib.CollapseUserControl' contains externally visible field 'HierachyLib.CollapseUserControl.CurrentItemProperty'.  Fields can be exposed only by structures.

更新1 - 全班的源代码

public sealed partial class CollapseUserControl : UserControl, IHierarchyHeightFix
{
    public MyItem CurrentItem
    {
        get { return (MyItem)GetValue(CurrentItemProperty); }
        set { SetValue(CurrentItemProperty, value); }
    }

    // Using a DependencyProperty as the backing store for CurrentItem.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CurrentItemProperty =
        DependencyProperty.Register("CurrentItem", typeof(MyItem), typeof(CollapseUserControl), new PropertyMetadata(null));

    Boolean viewState = true;

    public CollapseUserControl()
    {
        this.DataContext = CurrentItem;
        this.InitializeComponent();
        this.Loaded += CollapseUserControl_Loaded;
    }

    void CollapseUserControl_Loaded(object sender, RoutedEventArgs e)
    {
        LoadData();
        if (this.Height.Equals(double.NaN))
        {
            this.Height = 50;
        }
        //this.Height = 50;
        //this.Width = double.NaN;
    }

    private void LoadData()
    {
        if (CurrentItem != null)
        {
            if (CurrentItem.IsValueControl)
            {
                ChildItemContainer.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                ValueItem.Visibility = Windows.UI.Xaml.Visibility.Visible;

                ValueItem.Text = CurrentItem.Value;
            }
            else
            {
                ChildItemContainer.Visibility = Windows.UI.Xaml.Visibility.Visible;
                ValueItem.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

                ChildItems.ItemsSource = CurrentItem.Childs;
                //foreach (MyItem item in CurrentItem.Childs)
                //{
                //    CollapseUserControl control = new CollapseUserControl();
                //    control.CurrentItem = item;
                //    ChildItems.Items.Add(control);
                //}
                ChildItems.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
        }

    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        if (viewState)
        {
            ChildItems.Visibility = Windows.UI.Xaml.Visibility.Visible;
            //show.Begin();
        }
        else
        {
            //hide.Begin();
            ChildItems.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }
        viewState = !viewState;
    }

    private void hide_Completed_1(object sender, object e)
    {
        ChildItems.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
    }

    private void show_Completed_1(object sender, object e)
    {
    }
}

更新2:XAML代码

<UserControl
x:Class="HierachyLib.CollapseUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HierachyLib"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>

    <DataTemplate x:Key="ReviewsItemsTemplate">
        <StackPanel Margin="0,0,0,20">
            <TextBlock Text="TEST" />
            <TextBlock Text="TEST"/>
        </StackPanel>
    </DataTemplate>
    <ItemsPanelTemplate x:Key="ReviewsItemsPanelTemplate">
        <StackPanel Margin="0,0,0,0" Width="Auto"/>
    </ItemsPanelTemplate>
</UserControl.Resources>
<!--xmlns:my="clr-namespace:HierarchyCollapse"-->
<Grid>
    <Grid Name="ChildItemContainer">
        <Grid.RowDefinitions>
            <RowDefinition Height="40" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Rectangle Grid.Row="0" Margin="0,0,70,0" Fill="Transparent" Canvas.ZIndex="4"/>
        <ListView HorizontalContentAlignment="Stretch" Background="#FF6599CD" CanDragItems="False" CanReorderItems="False" Grid.Row="0"
                  ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollMode="Disabled" 
                  ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.HorizontalScrollMode="Disabled">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListViewItem">
                                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Height="40">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="50" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="Sample Text" FontSize="17" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Canvas.ZIndex="10"/>
                                        <Image PointerPressed="Button_Click_1" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center" Source="Assets/arrw_right.png" Stretch="None" Margin="0,0,10,0"/>
                                    </Grid>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
            <ListViewItem />
        </ListView>
        <ListView Grid.Row="1"
            IsHitTestVisible="True"
            CanDragItems="True" 
            CanReorderItems="True" 
            AllowDrop="True" 
            Name="ChildItems" 
            SelectionMode="None"
            IsItemClickEnabled="False">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <local:CollapseUserControl CurrentItem="{Binding RelativeSource={RelativeSource Self}}" />
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Padding" Value="0"/>
                    <Setter Property="Margin" Value="0"/>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>
    </Grid>
    <TextBlock Name="ValueItem" Margin="0,0,0,0" Height="40" FontSize="36"></TextBlock>
</Grid>

我得到的新错误:

Failed to create a 'Windows.UI.Xaml.PropertyPath' from the text ''. 

错误来自<local:CollapseUserControl CurrentItem="{Binding RelativeSource={RelativeSource Self}}" />

2 个答案:

答案 0 :(得分:9)

似乎你可以将你的财产标记为内部,然后它开始工作......

internal static readonly DependencyProperty CurrentItemProperty...

编辑*

一个更好的方法似乎是平台控件所做的是拥有一个暴露DependencyProperty对象的实际CLR属性 - 如下所示:

private static readonly DependencyProperty CurrentItemPropertyField =
    DependencyProperty.Register/RegisterAttached(...);

internal static DependencyProperty CurrentItemProperty
{
    get
    {
        return CurrentItemPropertyField;
    }
}

这允许Blend等工具发现属性。

答案 1 :(得分:1)

您可以使用自动属性来设置默认值(来自c#5),如下所示:

public static DependencyProperty CurrentItemPropertyField { get; } = 
        DependencyProperty.Register("Value", typeof(string), typeof(MyControl), new PropertyMetadata("{No Value}"));

也可用于UWP应用程序