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

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

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

最佳答案

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

internal static readonly DependencyProperty CurrentItemProperty...

编辑*

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

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

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

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

关于c# - 无法在 Windows 运行时组件库中的 UserControl 中创建依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15061659/

相关文章:

c# - 是否可以用交替的垂直列填充 DataGridView?

c# - 如何让日期选择器默认为 "Pick a Date"?

c# - 在 Windows 8 XAML 中创建项目填充到控件宽度的水平列表?

java - 使用 JNA 从 Java 调用 Windows UWP API

c++ - 在 Windows 8 上使用 Windows UI 自动化列出 Metro 应用程序的所有 UI 元素

c# - Visual Studio 2015 智能感知在调试期间未出现

c# - 存储对值类型的引用?

silverlight - Windows Phone 7 silverlight 用户控制 : data binding not working on custom property

c# - 命令行参数参数限制

c# - 继承的时候会包含require的https吗?