2015-12-15 52 views
1

我想換一個PivotItem頭的於地面的顏色,當鼠標移動到與UWP,我的工作與Windows 10UWP - PivotItem鼠標移到變色

我試過這段代碼:

<Page.Resources> 
     <SolidColorBrush x:Key="mouseOverColor" 
        Color="Red" /> 
    </Page.Resources> 
<Pivot HeaderTemplate="{StaticResource HeaderTemp}" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="10,0,10,0"> 
      <VisualStateManager.VisualStateGroups> 
       <VisualStateGroup> 
        <VisualState x:Name="Narrow"> 
         <VisualState.StateTriggers> 
          <AdaptiveTrigger MinWindowWidth="0" /> 
         </VisualState.StateTriggers> 
         <VisualState.Setters> 
          <Setter Target="Icon.(RelativePanel.AlignHorizontalCenterWithPanel)" Value="True" /> 
          <Setter Target="LabelText.(RelativePanel.Below)" Value="Icon" /> 
          <Setter Target="LabelText.(RelativePanel.AlignHorizontalCenterWith)" Value="Icon" /> 
         </VisualState.Setters> 
        </VisualState> 
        <VisualState x:Name="Wide"> 
         <VisualState.StateTriggers> 
          <AdaptiveTrigger MinWindowWidth="500" /> 
         </VisualState.StateTriggers> 
         <VisualState.Setters> 
          <Setter Target="Icon.(RelativePanel.AlignVerticalCenterWithPanel)" Value="True" /> 
          <Setter Target="LabelText.(RelativePanel.RightOf)" Value="Icon" /> 
          <Setter Target="LabelText.(RelativePanel.AlignVerticalCenterWith)" Value="Icon" /> 
          <Setter Target="RelativePanel.Margin" Value="0,0,12,0"/> 
          <Setter Target="Icon.Margin" Value="0,0,0,0"/> 
         </VisualState.Setters> 
        </VisualState> 
       </VisualStateGroup> 
      </VisualStateManager.VisualStateGroups> 
      <Pivot.Resources> 
       <Style x:Key="myStyle" TargetType="PivotItem"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="PivotItem"> 
           <ControlTemplate.Triggers> 
            <Trigger Property="IsMouseOver" 
        Value="True"> 
             <Setter Property="Background" 
        Value="{StaticResource mouseOverColor}" /> 
            </Trigger> 
           </ControlTemplate.Triggers> 
           <ContentPresenter /> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </Pivot.Resources> 
      <PivotItem Header="/images/img.png" Margin="12,10,12,0" > 
       <Grid> 
        ..... 
       </Grid> 
      </PivotItem> 

我有錯誤「在'ControlTemplate'類型中找不到'可附加屬性'觸發器' 請您有任何想法如何在鼠標在通用應用程序中結束時更改圖像的顏色 感謝您的幫助

+1

創建兩張圖片,堆疊它們並根據鼠標顯示/隱藏可能更容易。或者製作一張圖片,爲前景部分使用透明顏色,將其疊加在矩形的頂部,並根據鼠標懸停更改矩形的填充。 –

回答

2

你想cha GE圖像的顏色,我認爲它不能在XAML設計來完成,你可以做這樣的:

XAML代碼:

<Pivot> 
     <PivotItem> 
      <PivotItem.Header> 
       <Image x:Name="headerimg" Source="Assets/1.jpg" PointerEntered="pointerEntered" PointerExited="pointerExited"/> 
      </PivotItem.Header> 
     </PivotItem> 
    </Pivot> 

關鍵在cs文件後面代碼:

private async void pointerEntered(object sender, PointerRoutedEventArgs e) 
{ 
    StorageFile imgFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets\\1.jpg")); 
    using(IRandomAccessStream streamIn = await imgFile.OpenReadAsync()) 
    { 
     //decoder the img 
     BitmapDecoder decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.JpegDecoderId, streamIn); 
     //get pixel 
     PixelDataProvider proved = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, new BitmapTransform(), 
      ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); 
     byte[] srcData = proved.DetachPixelData(); 

     // GRAYSCALE 
     for (int i = 0; i < srcData.Length; i += 4) 
     { 
      double b = srcData[i]; //B 
      double g = srcData[i + 1]; //G 
      double r = srcData[i + 2]; //R 
      //average 
      double v = (r + g + b)/3d; 
      //replace old rgb 
      srcData[i] = srcData[i + 1] = srcData[i + 2] = Convert.ToByte(v); 
     } 

     WriteableBitmap wbimg = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight); 
     srcData.CopyTo(wbimg.PixelBuffer); 
     this.headerimg.Source = wbimg; 
    } 

} 

private async void pointerExited(object sender, PointerRoutedEventArgs e) 
{ 
    StorageFile imgFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets\\1.jpg")); 
    IRandomAccessStream streamIn = await imgFile.OpenReadAsync(); 
    BitmapImage bitmap = new BitmapImage(); 
    bitmap.SetSource(streamIn); 
    this.headerimg.Source = bitmap; 
} 

要更改圖像的顏色,您需要對此圖像進行解碼,獲取其值RGB,重寫這些值並將它們變回圖像。 Image控件沒有Foreground屬性。

+0

感謝GraceF的回覆,我在回覆中只有一個問題,如果我有4張圖片,那麼我應該使用一個數組,然後對數組中的每張圖片應用解碼? – user3821206

+1

使用數組表示什麼?你的意思是使用數組來收集四個圖像的地址?每次你的鼠標在標題上,你想一起改變4個圖像的顏色?或每次更改一個圖像?無論如何,你可以將這個'changecolor'代碼包裝到一個函數中,並且每次獲取圖像文件時調用該函數。 –

+0

好的,謝謝GraceF,我將使用一個函數,我有4個圖像我想包裹每個圖像的變化顏色 – user3821206