silverlight - Silverlight 4 中的自定义网格样式

标签 silverlight coding-style silverlight-4.0 background

我想使用样式设置网格的背景。我设置网格的背景属性。

但是我有一个用 LinearGradientFill 填充的边框和一个其中也有 LinearGradientFill 的路径。

但我无法将两者结合起来。

下面是示例代码。我想将其创建为一种样式。

<Grid>
<Border BorderBrush="Black" BorderThickness="2">
                <Border.Background>
                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                        <GradientStop Color="Black" Offset="0.953" />
                        <GradientStop Color="White" Offset="0" />
                    </LinearGradientBrush>
                </Border.Background>
            </Border>
            <Path Data="M 0,0 C 0,620 10,10 560,0" Height="60" VerticalAlignment="Top">
                <Path.Fill>
                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                        <GradientStop Color="Black" Offset="0" />
                        <GradientStop Color="White" Offset="0.779" />
                    </LinearGradientBrush>
                </Path.Fill>
            </Path>
</Grid>

它给我一个错误:

The Property 'Value' is set more than once.

最佳答案

阿奇,

您需要使用模板才能将任意 XAML 放入样式中。不幸的是,只有控件有模板,网格和边框不是控件。但有一个解决方案。尽管没有您希望的那么干净,但以下 XAMl 应该可以实现您的目标。将以下 XAML 粘贴到 Charles Petzold's XAML Cruncher 中以查看结果:

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Width="400" Height="400">
    <UserControl.Resources>
        <!-- A ContentControl template that defines your background -->
        <ControlTemplate x:Key="BackgroundTemplate" TargetType="ContentControl">
            <Grid>
                <Border BorderBrush="Black" BorderThickness="2">
                    <Border.Background>
                        <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                            <GradientStop Color="Black" Offset="0.953" />
                            <GradientStop Color="White" Offset="0" />
                        </LinearGradientBrush>
                    </Border.Background>
                </Border>
                <Path Data="M 0,0 C 0,620 10,10 560,0" Height="60" VerticalAlignment="Top">
                    <Path.Fill>
                        <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                            <GradientStop Color="Black" Offset="0" />
                            <GradientStop Color="White" Offset="0.779" />
                        </LinearGradientBrush>
                    </Path.Fill>
                </Path>
            </Grid>
        </ControlTemplate>
        <!-- A ContentControl Style that references the background template -->
        <Style x:Key="BackgroundStyle" TargetType="ContentControl">
            <Setter Property="Template" Value="{StaticResource BackgroundTemplate}" />
        </Style>
    </UserControl.Resources>

    <!-- Typical usage; place the background ContentControl behind your body content -->
    <Grid x:Name="LayoutRoot">
        <ContentControl Style="{StaticResource BackgroundStyle}" />
        <TextBlock Text="Your Content" Foreground="Red" FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</UserControl>

关于silverlight - Silverlight 4 中的自定义网格样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2873374/

相关文章:

python - `pass` 标记缩进代码块结束的关键字

c# - 访问继承属性时是否应该使用 "base."前缀

c# - 如何从 ViewModel 开始 Storyboard?

wpf - 与 Silverlight 4 相比,WPF 还必须提供什么?

Silverlight DragDrop.DoDragDrop()

Javascript/Silverlight 代理双加载延迟

php/html编码风格

c# - 在 Visual Studio (C#) 中的编译时设置值?

c# - Silverlight 中的同步 WebClient 下载

wpf - ItemsControl MVVM 绑定(bind)