Silverlight 4,通过样式来控制用户控件内部

发布于 2024-10-21 16:36:24 字数 2119 浏览 1 评论 0原文

我创建了一个 UserControl,其中包含一个 AutoCompleteBox 来简化绑定。现在需求发生了变化,我需要将样式传递给 AutoCompleteBox。我将样式的 DependencyProperty 添加到我的 UserControl 中。绑定有效,但样式未应用。

这是我背后的代码:

public partial class CustomAutoCompleteBox
{
    public static readonly DependencyProperty ContentStyleProperty = DependencyProperty.Register(
                "ContentStyle",
                typeof(Style),
                typeof(CustomAutoCompleteBox),
                new PropertyMetadata(OnContentStyleChanged));

/// <summary>
/// Initializes a new instance of the <see cref="CustomAutoCompleteBox"/> class.
/// </summary>
public CustomAutoCompleteBox()
{
    this.InitializeComponent();
}

/// <summary>
/// Gets or sets ContentStyle.
/// </summary>
public Style ContentStyle
{
    get
    {
        return (Style)this.GetValue(ContentStyleProperty);
    }

    set
    {
        this.SetValue(ContentStyleProperty, value);
    }
}

private static void OnContentStyleChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    var customAutoCompleteBox = obj as CustomAutoCompleteBox;
    var newValue = e.NewValue as Style;
    if (customAutoCompleteBox != null && newValue != null)
    {
        customAutoCompleteBox.ContentStyle = newValue;
    }
}

和xaml:

<Grid x:Name="LayoutRoot">
<Input:AutoCompleteBox Style="{Binding ContentStyle}"
                       MinimumPrefixLength="0"
                       ItemTemplate="{StaticResource DescriptionItemTemplate}"
                       ValueMemberBinding="{Binding Description, Mode=TwoWay}"
                       SelectedItem="{Binding Value, ValidatesOnDataErrors=True, Mode=TwoWay}"
                       ItemsSource="{Binding Values}"
                       Text="{Binding Text, Mode=TwoWay}"
                       Behaviors:AutoCompleteBoxBehaviors.PopulatingCommand="{Binding PopulationCommand}"
                       Behaviors:AutoCompleteBoxBehaviors.ItemFilterPredicate="{Binding ItemFilterPredicate}"/>

</Grid>

我希望有人能指出我做错了什么。

干杯 交流电

I created a UserControl which contains an AutoCompleteBox to simplify the bindings. Now requirements have changed and I need to pass a style to the AutoCompleteBox. I added a DependencyProperty for the style to my UserControl. The binding works but the styling is not applied.

This is my code behind:

public partial class CustomAutoCompleteBox
{
    public static readonly DependencyProperty ContentStyleProperty = DependencyProperty.Register(
                "ContentStyle",
                typeof(Style),
                typeof(CustomAutoCompleteBox),
                new PropertyMetadata(OnContentStyleChanged));

/// <summary>
/// Initializes a new instance of the <see cref="CustomAutoCompleteBox"/> class.
/// </summary>
public CustomAutoCompleteBox()
{
    this.InitializeComponent();
}

/// <summary>
/// Gets or sets ContentStyle.
/// </summary>
public Style ContentStyle
{
    get
    {
        return (Style)this.GetValue(ContentStyleProperty);
    }

    set
    {
        this.SetValue(ContentStyleProperty, value);
    }
}

private static void OnContentStyleChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    var customAutoCompleteBox = obj as CustomAutoCompleteBox;
    var newValue = e.NewValue as Style;
    if (customAutoCompleteBox != null && newValue != null)
    {
        customAutoCompleteBox.ContentStyle = newValue;
    }
}

And the xaml:

<Grid x:Name="LayoutRoot">
<Input:AutoCompleteBox Style="{Binding ContentStyle}"
                       MinimumPrefixLength="0"
                       ItemTemplate="{StaticResource DescriptionItemTemplate}"
                       ValueMemberBinding="{Binding Description, Mode=TwoWay}"
                       SelectedItem="{Binding Value, ValidatesOnDataErrors=True, Mode=TwoWay}"
                       ItemsSource="{Binding Values}"
                       Text="{Binding Text, Mode=TwoWay}"
                       Behaviors:AutoCompleteBoxBehaviors.PopulatingCommand="{Binding PopulationCommand}"
                       Behaviors:AutoCompleteBoxBehaviors.ItemFilterPredicate="{Binding ItemFilterPredicate}"/>

</Grid>

I hope someone can point out what I'm doing wrong.

Cheers
AC

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

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

发布评论

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

评论(1

束缚m 2024-10-28 16:36:24

上面的代码将一个依赖属性添加到名为 ContentStyle 的自定义控件中。但是,您拥有的 XAML 会尝试将控件的样式绑定到 DataContext 上名为“ContentStyle”的值。这是两件不同的事情。

您想要的是能够以编程方式为控件分配新样式。不幸的是,这只能进行一次,因为一旦设置了样式就无法“取消设置”。这是来自 Silverlight 论坛。

http://forums.silverlight.net/forums/p/11670/37375.aspx

但是,可以设置一次样式,因此解决此问题的最简单方法是按照帖子中的建议。以编程方式从控件树中删除自定义控件,设置新样式,然后将其添加回父控件。

The code you have above adds a dependency property to your custom control called ContentStyle. However, the XAML you have there would attempt to bind the Style of the control to a value on the DataContext called "ContentStyle". These are two different things.

What you want is to be able to assign a new style to the control programmatically. Unfortunately, this is only possible once, as there is no way to "unset" a style once it has been set. This is from the Silverlight forum.

http://forums.silverlight.net/forums/p/11670/37375.aspx

However, it IS possible to set the style once, so the easiest way to solve this problem is as suggested in the post. Programmatically remove your custom control from the control tree, set your new style(s), and add it back to the parent control.

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