Silverlight:UserControl 有背景属性吗?
我在 Silverlight 类库中创建了一个 BarMenuItem
UserControl
,并尝试在我的主 Silverlight 应用程序中使用。
BarMenuItem.xaml:
<UserControl x:Class="ButtonControlLibrary.BarMenuItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
</UserControl>
BarMenuItem.xmal.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace ButtonControlLibrary
{
public partial class BarMenuItem : UserControl
{
public BarMenuItem()
{
InitializeComponent();
}
}
}
所以在MainPage中,我定义了xmlns:blib =“clr-namespace:ButtonControlLibrary; assembly = ButtonControlLibrary”
尝试使用BarMenuItem
:
<blib:BarMenuItem Width="100" Height="150" Background="Red"/>
编译并运行它,我期望看到红色背景,但我什么也没看到。
发生什么事了?
我很困惑。我在谷歌上搜索了很多,发现有一个解决方法,但非常丑陋:在UserControl
内放置一个Grid
容器,然后绑定其Background
属性到UserControl
的Background
:
Background="{Binding Background, ElementName=guiUserControl}"
但这不是解决方案。请指教。
在 Adobe Flex 中,更改 UserControl 的背景是很自然的,就像我在上面的代码中所做的那样。
我错过了什么吗?
谢谢。
I created a BarMenuItem
UserControl
in a Silverlight class library, and try to used in my main Silverlight application.
BarMenuItem.xaml:
<UserControl x:Class="ButtonControlLibrary.BarMenuItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
</UserControl>
BarMenuItem.xmal.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace ButtonControlLibrary
{
public partial class BarMenuItem : UserControl
{
public BarMenuItem()
{
InitializeComponent();
}
}
}
So in the MainPage, I have xmlns:blib="clr-namespace:ButtonControlLibrary;assembly=ButtonControlLibrary"
defined
Tried to use BarMenuItem
:
<blib:BarMenuItem Width="100" Height="150" Background="Red"/>
Compile and run it, and I expected to see a Red background, but I see nothing.
What happenend?
I'm so confused. I googled a lot, finding there is a workaround, but very ugly: Put a Grid
container inside the UserControl
, and then binding its Background
property to the UserControl
's Background
:
Background="{Binding Background, ElementName=guiUserControl}"
But this is not the solution. Please advice.
In Adobe Flex, it's so natural to change a UserControl's background like what I do in the above code.
Am I missing anything?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该属性在那里,但似乎在 Silverlight 中不起作用。您的下一个最佳选择是您所说的您已经知道的解决方案。将layoutroot 的背景与用户控件的背景绑定。
The property is there but it doesn't seem to work in Silverlight. Your next best bet is the solution you said you already know. Binding layoutroot's background with usercontrol's background.