4

我想在用户控件中创建数据可绑定属性。此用户控件包含在“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}}" />.

4

2 回答 2

9

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

internal static readonly DependencyProperty CurrentItemProperty...

编辑*

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

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

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

这允许诸如 Blend 之类的工具发现属性。

于 2013-02-25T09:05:48.187 回答
2

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

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

也适用于 UWP 应用程序

于 2018-06-25T21:38:31.913 回答