2011-04-04 83 views
0

我有一個名爲GraphPanel的用戶控件。它有兩個依賴屬性,一個是自定義的PanelTitle,另一個是從FrameworkElement,Content繼承的。Silverlight:綁定到UserControl的依賴項屬性

public static readonly DependencyProperty PanelTitleProperty = DependencyProperty.Register(
     "PanelTitle", 
     typeof(string), 
     typeof(GraphPanel), 
     new PropertyMetadata("") 
    ); 
    // ... 
    public string PanelTitle 
    { 
     set { SetValue(PanelTitleProperty, value); } 
     get { return (string)GetValue(PanelTitleProperty); } 
    } 

的XAML代碼如下:

<UserControl 
    x:Class="PlaceringsGuiden.Library.Components.GraphPanel" 
    DataContext="{Binding RelativeSource={RelativeSource self}}"> 

    <UserControl.Resources> 
     <ResourceDictionary Source="/App;component/Assets/Styles/GraphPanelStyles.xaml" /> 
    </UserControl.Resources> 

    <Border Style="{StaticResource GraphPanelBorderStyle}"> 
     <Grid Style="{StaticResource GraphPanelGridStyle}"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition /> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="1*" /> 
       <RowDefinition Height="8*" /> 
      </Grid.RowDefinitions> 
      <Grid Grid.Column="0" Grid.Row="0"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="0.02*" /> 
        <ColumnDefinition Width="1*" /> 
        <ColumnDefinition Width="0.02*" /> 
       </Grid.ColumnDefinitions> 
       <TextBlock Grid.Column="1" 
          Grid.Row="0" 
          Text="{Binding Path=PanelTitle}" 
          Style="{StaticResource GraphPanelHeaderStyle}" /> 
      </Grid> 

      <Grid Grid.Column="0" Grid.Row="0" x:Name="GraphPanelContentPresenter"> 
       <ContentPresenter Content="{Binding Path=Content}" /> 
      </Grid> 
     </Grid> 
    </Border> 
</UserControl> 

運行此產生一個例外:

Value does not fall within the expected range. 
    at MS.Internal.XcpImports.CheckHResult(UInt32 hr) 

我在做什麼錯?我應該怎麼做才能實現這種綁定?

謝謝!

回答

0

我通過從ContentPresenter中刪除綁定來解決此問題。也就是說,這個解決方案存在缺陷,因爲自定義控件不是元素容器。

通過創建一個擴展ContentControl的新類,使用ControlTemplate進行樣式設計,可以實現此功能,而無需複雜的綁定方案。

public class GraphPanel : ContentControl 
{ 
    #region Properties 
    public string PanelTitle 
    { 
     get { return (string) GetValue(PanelTitleProperty); } 
     set { SetValue(PanelTitleProperty, value); } 
    } 
    #endregion 

    #region Dependency properties 
    public static readonly DependencyProperty PanelTitleProperty = 
     DependencyProperty.Register("PanelTitle", typeof(string), typeof(GraphPanel), new PropertyMetadata("")); 
    #endregion 

    public GraphPanel() 
     : base() 
    { 
     DefaultStyleKey = typeof(GraphPanel); 
    } 
} 

和XAML代碼:

<Style TargetType="local:GraphPanel"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:GraphPanel"> 
       <Border Style="{StaticResource GraphPanelBorderStyle}"> 
        <Grid Style="{StaticResource GraphPanelGridStyle}"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition /> 
         </Grid.ColumnDefinitions> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="1*" /> 
          <RowDefinition Height="8*" /> 
         </Grid.RowDefinitions> 
         <Grid Grid.Column="0" Grid.Row="0"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="0.02*" /> 
           <ColumnDefinition Width="1*" /> 
           <ColumnDefinition Width="0.02*" /> 
          </Grid.ColumnDefinitions> 
          <TextBlock Grid.Column="1" 
         Grid.Row="0" 
         Text="{TemplateBinding PanelTitle}" 
         Style="{StaticResource GraphPanelHeaderStyle}" /> 
         </Grid> 

         <Grid Grid.Column="0" Grid.Row="1"> 
          <ContentPresenter /> 
         </Grid> 
        </Grid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

有時,它只是幫助把它寫下來,你會意識到自己的錯誤。感謝您給予這一刻的想法!