【问题标题】:Silverlight: Binding to UserControl's dependency propertiesSilverlight:绑定到 UserControl 的依赖属性
【发布时间】:2011-07-29 01:59:41
【问题描述】:

我有一个名为 GraphPanel 的用户控件。它有两个依赖属性,一个是自定义的 PanelTitle,另一个是从 FrameworkElement 继承的 Content。

    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)

我做错了什么?我应该怎么做才能实现这种绑定?

谢谢!

【问题讨论】:

    标签: silverlight-4.0 binding dependency-properties


    【解决方案1】:

    我通过从 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>
    

    有时它只是有助于写下来,你会意识到自己的错误。感谢您考虑一下!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2017-12-24
      • 2014-08-31
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      相关资源
      最近更新 更多