Silverlight:将子控件属性绑定到用户控件中的属性 [英] Silverlight: Binding a child controls property to a property in a user control
问题描述
如果我定义了用户控件:
If I have a user control defined:
public partial class MainFooter : UserControl
{
public System.Windows.Media.Color BkColor;
}
它是xaml:
<UserControl x:Class="Test.MainFooter">
<Grid x:Name="LayoutRoot">
<Rectangle x:Name="rctBottom_Background2"
HorizontalAlignment="Stretch"
Grid.Row="2">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.82,0.895" StartPoint="0.911,-0.442">
<GradientStop Color="{**How can I bind this to the BkColor property?}"/**>
<GradientStop Color="#00FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</UserControl>
并使用:
<MyControls:MainFooter x:Name="rcrMainFooter"
BkColor="#FFE2B42A">
</MyControls:MainFooter>
如何将矩形中的GradientStop Color绑定到用户控件BkColor的值属性?
How would I go about binding the GradientStop Color in the Rectangle to the value of the it's user controls BkColor property?
推荐答案
通常,当我看到这个问题带来的答案是您必须在代码中完成时,在我看来, Silverlight绑定不支持此功能-因此您必须通过手动设置属性来完全手动完成 。但是事实并非如此:
Often when I've seen this question posed the answer is 'you have to do it in code', which sounded to me like 'Silverlight binding doesn't support this' - so you have to do it 'completely manually' by setting the property by hand. But thats not the case :
Silverlight绑定确实支持此功能-只是Silverlight XAML不支持。
以下是 UserControl
的示例,该示例基本上包装了 DataForm
。在构造函数中,运行可以绑定到用户控件属性的绑定。希望他们将来改变对此的XAML支持,那么回来并进行修复将很简单。
Here's an example of a UserControl
that basically wraps a DataForm
. In the constructor you run the binding which can bind to your 'user control property'. Hopefully if they change the XAML support for this in future then it'll be trivial to come back and fix.
App.xaml
<AddressControl MyHeader="Shipping Address"/>
AddressControl.xaml
<UserControl>
<DataForm Name="dfAddress" Header="BOUND IN CODE"/>
</UserControl>
可选:表示您已将代码中的值与评论
Optional: indidicate that you've bound the value in code with a comment
AddressControl.xaml.cs
publicAddressControl()
{
InitializeComponent();
// bind the HeaderProperty of 'dfAddress' to the 'MyHeader' dependency
// property defined in this file
dfAddress.SetBinding(DataForm.HeaderProperty,
new System.Windows.Data.Binding { Source = this,
Path = new PropertyPath("MyHeader") });
}
// standard string dependency property
public string MyHeader
{
get { return (string)GetValue(MyHeaderProperty); }
set { SetValue(MyHeaderProperty, value); }
}
public static readonly DependencyProperty MyHeaderProperty =
DependencyProperty.Register("MyHeader", typeof(string),
typeof(AddressControl), null);
这会将 MyHeader
属性绑定到我的 AddressControl
用户控件添加到数据表单上的 Header
属性。我将其设置为我的仅是为了易于阅读-但实际上我在实际代码中仅使用了标头。
This binds the MyHeader
property on my AddressControl
usercontrol to the Header
property on the dataform. I made it 'My' solely for easy readability - but I'm actually using just 'Header' in my real code.
真遗憾,我们仍然无法做到这一点XAML中,但它比我最初尝试的要好得多,那就是捕获 DataContextChanged
事件,然后手动设置。
Real shame we still can't do this in XAML, but its better than what I first attempted which was to capture the DataContextChanged
events and then manually set things.
这篇关于Silverlight:将子控件属性绑定到用户控件中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!