Silverlight:UserControl 有背景属性吗?

发布于 2024-12-10 07:37:30 字数 1794 浏览 0 评论 0原文

我在 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属性到UserControlBackground

Background="{Binding Background, ElementName=guiUserControl}"

但这不是解决方案。请指教。

在 Adob​​e 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单爱 2024-12-17 07:37:30

该属性在那里,但似乎在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文