在Silverlight中绑定用户控件的背景 [英] Binding background of a usercontrol in Silverlight

查看:82
本文介绍了在Silverlight中绑定用户控件的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,你能帮我吗?

我正在使用Silverlight 4和VS2010.

我创建了一个非常简单的UserControl,名为"Marquee".

Hy guys, can You help me on this?

I''m using Silverlight 4 and VS 2010.

I''ve created a very simple UserControl named "Marquee".

<usercontrol>
	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" x:Name="userControl"
	mc:Ignorable="d"
	xmlns:util="clr-namespace:CanvasClip"
	x:Class="Marquee.Marquee"
	d:DesignWidth="1125" d:DesignHeight="30">

	<grid x:name="LayoutRoot" xmlns:x="#unknown">
		<canvas x:name="MarqueeContainer" margin="0" util:clip.tobounds="true" xmlns:util="#unknown">
			<stackpanel x:name="MarqueeContent" d:layoutoverrides="VerticalMargin" orientation="Horizontal" canvas.left="0" height="30" loaded="MarqueeContent_Loaded" xmlns:d="#unknown" />
		</canvas>
	</grid>
</usercontrol>


在我的主页中,我已经插入了用户控件"Marquee1"并设置了它的背景属性.


In my MainPage I''ve inserted the user control "Marquee1" and set it''s background property.

<grid x:name="LayoutRoot" xmlns:x="#unknown">
    <mrq:marquee x:name="Marquee1" height="30" verticalalignment="Top" margin="148,0,413,0" removed="#FFC8DC1A" xmlns:mrq="#unknown" />
    <Button Content="Button" Height="30" HorizontalAlignment="Left" Margin="393,134,0,0" x:Name="button1" VerticalAlignment="Top" Width="219" Click="button1_Click" />
</grid>


(不知为什么我不能像在VS上那样粘贴代码.它说已删除"的地方应该在mrq:Maquee元素上是背景",而xmlns:mrq =#unknown"的添加对我来说很奇怪. )

我需要的是将用户控件"Marquee"中的画布"MarqueeContainer"背景绑定到MainPage上设置的"Marquee1"背景属性.

在用户控件XAML中,它必须类似于:
Background ="{Binding ???????}

但是我有点迷茫!


(Somehow I can''t paste the code as it was on VS. Where it say "removed" it should be "background" on the mrq:Maquee element and the xmlns:mrq="#unknown" additions are strange to me.)

What I need is to bind the canvas "MarqueeContainer" background in the usercontrol "Marquee" to the "Marquee1" background property set on the MainPage.

In the usercontrol XAML It has to be something like:
Background="{Binding ???????}

But I''m kind of lost!

推荐答案

我发现C#中的一种解决方法可以放在usercontrol的Loaded事件中:
I have found a workaround in C# to be placed in the Loaded event of the usercontrol:
MarqueeContainer.Background = ((Marquee)LayoutRoot.Parent).Background;



它可以工作...但是我认为XAML将会成为理想的地方.



It works ... but I think that XAML would be the place to be.


您可以使用元素到元素的绑定.

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

LayouRoot在此实例中是您的应用程序的网格.
You could use element to element binding.

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

LayouRoot being the grid of your application in this instance.


在所有地方,Kelvin,谢谢.

可以使用:
Allmost there Kelvin, thanks.

Got it working with:
Background="{Binding Background, ElementName=userControl}"


这篇关于在Silverlight中绑定用户控件的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆