开发者

Silverlight: Does UserControl Have a Background Property?

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"开发者_StackOverflow
    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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜