WPF問題。我不知道要谷歌什麼。如何將Control和ContentControl的繼承屬性「傳遞」給UserControl中的元素?
我有一個用戶控件:
<UserControl x:Class="MyProj.MyControl"
x:Name="Self"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button />
</UserControl>
我用這樣的:
<local:MyControl Background="Green" />
但背景似乎並沒有改變。這是因爲按鈕的背景沒有改變。我想讓按鈕的背景使用usercontrol的背景。
我想我可以做到這一點:
<UserControl x:Class="MyProj.MyControl"
x:Name="Self"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button Background="{Binding Path=Background, ElementName=Self" />
</UserControl>
但我非常希望所有的控制和ContentControl中繼承屬性應用到按鈕(工具提示了borderThickness,BorderBrush等)。
所以我能做些什麼,而不是這樣的:
<UserControl x:Class="MyProj.MyControl"
x:Name="Self"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button Background="{Binding Path=Background, ElementName=Self"
ToolTip="{Binding Path=ToolTip, ElementName=Self"
BorderThickness="{Binding Path=BorderThickness, ElementName=Self"
BorderBrush="{Binding Path=BorderBrush, ElementName=Self"
...
...
...
... />
</UserControl>
?
(注:這是它承載許多控件較大的用戶控件的小(最小的我可以管理,其實)的例子。)
如果你要做的就是主機一個按鈕,那麼你爲什麼不插入到你的其他XAML直接?否則,當您託管更多的控件(通常情況下),您必須按照您的示例進行操作,因爲正如您所知,控件是不尋常的。 – Davio
這是一個較小的(實際上我可以管理的最小的)較大的UserControl的示例,它承載了許多控件。 – epalm
它是否需要成爲一個UserControl?爲什麼不僅僅是一個樣式模板或ContentControl與您的依賴關係帶入TemplateBindings? –