Silverlight:绑定到 UserControl 的依赖属性

发布于 2024-10-30 03:53:01 字数 2331 浏览 2 评论 0原文

我有一个名为 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)

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

谢谢!

I have an user control named GraphPanel. It has two dependency properties, one custom, PanelTitle, and the other inherited from the 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); }
    }

The XAML code is as follows:

<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>

Running this yields an exception:

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

What am I doing wrong? What should I do to achieve this binding?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

也只是曾经 2024-11-06 03:53:01

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

有时它只是有助于将其写下来,并且您会意识到自己的错误。不过还是感谢您考虑一下!

I resolved this issue by removing the binding from the ContentPresenter. That said, this solution is flawed as the custom control is not an element container.

By creating a new class extending ContentControl, styling with ControlTemplate, is this achieved without the need of complicated binding scenarios.

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);
    }
}

and XAML code:

<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>

Sometimes it just helps writing it down, and you'd realise your own mistakes. Thanks for giving this a moment's thought though!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文