代码之家  ›  专栏  ›  技术社区  ›  Thomas

以Silverlight路径样式设置数据属性

  •  2
  • Thomas  · 技术社区  · 15 年前

    我正在尝试将尽可能多的路径元素属性放入样式中,这是可行的,只要我不向样式设置器添加数据:

    <UserControl x:Class="Demo.Controls.SilverlightControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Width="400" Height="300">
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.Resources>
                <Style x:Name="PathStyle" TargetType="Path">
                    <Setter Property="Data" Value="0,0 L1,0"></Setter>
                    <Setter Property="Stroke" Value="Blue"></Setter>     
                    <Setter Property="Stretch" Value="Fill"></Setter>
                </Style>
    
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Path Grid.Row="0"
                   Height="7"               
                   Data="M0,0 L1,0"
                   Stretch="Fill"
                   Stroke="Black"/>
            <Path Grid.Row="1"
                   Height="7"               
                   Style="{StaticResource PathStyle}"/>
        </Grid>
    </UserControl>
    

    如果打开这个示例,您将看到第一条路径没有问题,但第二条路径会导致 AG_E_UKNOWN_ERROR 在Visual Studio 2008中。

    是否可以在样式中定义路径的数据?

    2 回复  |  直到 12 年前
        1
  •  1
  •   Alexander Hitesh Bavaliya    12 年前

    这应该有效:

    <Style x:Name="PathStyle" TargetType="Path">
        <Setter Property="Data" Value="M0,0 L1,0"/>
        <Setter Property="Stroke" Value="Blue"/>
        <Setter Property="Stretch" Value="Fill"/>
    </Style>
    
        2
  •  0
  •   Dave Haigh    12 年前

    在样式中定义数据属性只会导致使用所呈现样式的第一个元素。其余的不会渲染。您需要执行以下操作:

    <Style x:Name="PathStyle" TargetType="Path"> 
    <Setter Property="Stroke" Value="Blue"/> 
    <Setter Property="Stretch" Value="Fill"/> 
    </Style> 
    
    <Path Style="{StaticResource PathStyle}" Data="M0,0 L1,0" /> 
    <Path Style="{StaticResource PathStyle}" Data="M0,0 L1,0" /> 
    <Path Style="{StaticResource PathStyle}" Data="M0,0 L1,0" /> ...