2013-10-09 40 views
0

我在窗口上有2个按钮。我正在应用这种风格。但得到了这个例外。在wpf中的按钮上应用样式时出现异常

'Set property'System.Windows.FrameworkElement.Style'throw a exception。'行号“38”和行位置“7”。

我的代码。

<Window.Resources> 
     <Image x:Key="btnconnect" Source="images/img-1.png" /> 
     <Image x:Key="btnshow" Source="images/img-2.png" /> 
     <Image x:Key="btnclick" Source="images/img-2clicked.png"/> 
     <Style x:Key="buttonconnect" TargetType="Button"> 
      <Setter Property="Content" Value="{StaticResource btnconnect}"> 
      </Setter> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Grid x:Name="grid"> 
          <Border x:Name="border" CornerRadius="8" BorderThickness="2"> 
           <ContentPresenter HorizontalAlignment="Center" 
          VerticalAlignment="Center" 
          TextElement.FontWeight="Bold"></ContentPresenter> 
          </Border> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsPressed" Value="true"> 
           <Setter Value="{StaticResource btnclick}" /> 
          </Trigger> 
          <Trigger Property="IsEnabled" Value="False"> 
           <Setter Property="Opacity" TargetName="grid" Value="0.25"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
     <Style x:Key="buttonshow" TargetType="{x:Type Button}"> 
      <Setter Property="Content" Value="{StaticResource btnshow}" /> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <Button Margin="50 100 50 100" Style="{StaticResource buttonconnect}" Height="50" Width="120"/> 
     <Button Margin="50 110 50 10" Style="{StaticResource buttonshow}" Height="50" Width="120"/> 
    </Grid> 

回答

1
<Setter Value="{StaticResource btnclick}" /> 

我想你忘了指定在其上的setter应用属性。

虽然我不确定它会导致运行时错误。

基本上你应该做的(理想情况下发布之前)是尽量删除尽可能多的代码,同时仍然能够重现你的问题,只有然后发布你的问题与简化的代码,如果你仍然没有线索在哪里错误来自于。

+0

好的,先生。你的回答是对的。告诉我一件事。这次我使用单个网格。如果我有一个嵌套网格或另一个孩子在网格内。在这种情况下,我如何访问它。目前<网格X:名称= “网格”> <边界X:名称= “边界” CornerRadius = “8” 了borderThickness = “2”>

+0

先生,如果我的回答是对的,请将其标记为'答复'。如果您有任何其他问题,请创建另一个问题(在您尝试并完成一些研究之后:令人惊讶的是,您可以通过从Panel继承的Grid.Children属性访问Grid的子节点) – franssu

+0

好的,谢谢你的帮助。 –