2017-07-31 68 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},只要填充是用户控件的属性。

相关问题