Silverlight 4,通过样式来控制用户控件内部
我创建了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面的代码将一个依赖属性添加到名为 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.