2017-07-31 65 views
0

我正在開發UWP應用程序,並且我創建了新的用戶控件,並且想要將它綁定到控件的代碼中的依賴項屬性(不帶datacontext)。將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背後(數據上下文應持有不同的數據,所以我可以這裏不使用它)

我如何在UWP中做到這一點?

回答

0

x:Bind將在此完美工作。注意x:Bind將尋找屬性,方法&在您的XAML的代碼隱藏中定義的事件。這是一個比ElementName更高性能的綁定。

<Path Fill="{x:Bind Fill, Mode=OneWay}" /> 
0

您應該能夠使用綁定的ElementName屬性繞過數據上下文,就像正常的WPF允許您執行的操作一樣。

如果屬性是你需要通過X指定名稱的用戶控件的一部分:名稱要在XAML用戶控件訪問它

<UserControl [...] x:Name="Control"...` 

然後使用類似{Binding ElementName=Control, Path=Fill},只要填充是用戶控件的屬性。

相關問題