2013-02-25 14 views
3

我想在用戶控件內創建數據可綁定屬性。此用戶控件包含「Windows運行時組件」項目。我使用下面的代碼來創建屬性。無法在Windows運行時組件庫中的UserControl中創建依賴項屬性

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}}" />

+0

你能展示你的班級的整個代碼嗎? – Guillaume 2013-02-25 07:06:06

+0

感謝您的快速回復。問題用類的源代碼更新。 – SachiraChin 2013-02-25 07:41:03

回答

8

好像你可以標記你的財產內部,然後開始工作......

internal static readonly DependencyProperty CurrentItemProperty... 

編輯*

更好的方法,這似乎是什麼平臺的控制做的是有一個公開DependencyProperty對象的實際CLR屬性 - 這樣的事情:

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

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

這樣的工具,如混合發現的屬性。

+0

非常感謝答案,它解決了我的問題,但現在我得到新的錯誤。首先,我創建的這個控件是遞歸的(我試圖創建treeview類型的用戶控件)。現在我從XAML得到這個錯誤「無法從文本''創建'Windows.UI.Xaml.PropertyPath'。我將更新有問題的xaml代碼。 – SachiraChin 2013-02-25 09:25:17

+0

另外我注意到我提到的新錯誤只是在「Windows運行時組件」項目上獲得。對於正常的Windows Store應用程序項目,您的解決方案完美無缺 – SachiraChin 2013-02-25 09:36:55

+0

我不知道。 WinRT組件是與.NET庫不同的野獸。他們需要從C++和JavaScript中消耗,所以還有很多額外的需求。 – 2013-02-25 09:44:30

相關問題