2013-03-18 93 views
0

我想在WPF中創建我自己的scrollviewer,除了RepeatButton中的背景圖像以外,它工作正常。如果我使用SolidColorBrush而不是ImageBrush,它可以正常工作。下面是我的xaml,我已經驗證了ImageSource可以通過將其添加到滾動條中的其他位置(特別是軌道圖像)中找到。你知道這是爲什麼嗎? forground圖像顯示正常。在ScrollBarViewer的RepeatButton中使用背景圖像不顯示圖像

<Style x:Key="ScrollBarButtonStyle" TargetType="RepeatButton"> 
    <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Width" Value="50"/> 
    <Setter Property="Height" Value="50"/>   
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="RepeatButton"> 
       <Grid Name="ButtonBackground"> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Background"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <ImageBrush ImageSource="{DynamicResource ListItemBackgroundBttnNormal}"/> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Pressed"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Background"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <ImageBrush ImageSource ="{DynamicResource ListItemBackgroundBttnPressed}"/> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Background"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <ImageBrush ImageSource = "{DynamicResource ListItemBackgroundBttnDisabled}"/> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
            <DoubleAnimation Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Opacity" From="1.0" To="0.5"/> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="MouseOver"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Background"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <ImageBrush ImageSource="{DynamicResource ListItemBackgroundBttnNormal}"/> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <ContentPresenter 
          x:Name="contentPresenter" 
          Content="{TemplateBinding Content}" 
          ContentTemplate="{TemplateBinding ContentTemplate}" 
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          Margin="{TemplateBinding Padding}"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

回答

0

嘗試將DynamicResource更改爲StaticResource並查看它是否仍然有效。如果未找到路徑,則StaticResource將不會顯示任何錯誤,而StaticResource將在此處執行此操作。我認爲你的圖像源在這方面是不知道的。 確保您在0123.x的App.xaml中添加資源。

+0

謝謝,就是這樣。我不得不將它添加到MergedDictionaries中。我不能將它作爲資源添加。 – user1701907 2013-03-19 13:50:51