[英]Binding UWP control to code behind property
我正在開發UWP應用程序,並創建了新的用戶控件,我想將其綁定到控件代碼中的依賴屬性(不包含數據上下文)。
背后的代碼:
public Brush Fill
{
get { return (Brush)GetValue(FillProperty); }
set { SetValue(FillProperty, value); }
}
// Using a DependencyProperty as the backing store for Fill. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FillProperty =
DependencyProperty.Register("Fill", typeof(Brush), typeof(MyControl), new PropertyMetadata(new SolidColorBrush(Colors.Black)));
XAML:
...
<Grid>
<Path Fill="{Binding ????}" Stretch="Fill">
...
</Path>
</Grid>
我希望我的路徑的fill屬性將綁定到后面代碼中的Fill
屬性(數據上下文應保存其他數據,因此我不能在此處使用它)
我如何在UWP中做到這一點?
x:Bind
可以很好地解決這個問題。 注意x:Bind
將尋找XAML背后代碼中定義的屬性,方法和事件。 它比ElementName
具有更高的性能。
<Path Fill="{x:Bind Fill, Mode=OneWay}" />
正如正常的WPF允許的那樣,您應該能夠使用綁定的ElementName屬性來規避數據上下文。
如果該屬性是用戶控件的一部分,則需要通過x:Name為xaml中的用戶控件分配名稱才能訪問它
<UserControl [...] x:Name="Control"...`
然后,使用{Binding ElementName=Control, Path=Fill}
,只要Fill是用戶控件的屬性即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.