UWP - PivotItem鼠标改变颜色

时间:2015-12-15 00:04:52

标签: c# xaml win-universal-app windows-10-universal

我希望在使用UWP鼠标结束时更改数据透视表标题的Forground颜色,我使用Windows 10

我试过这段代码:

<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>

我有一个错误&#34;可附加属性&#34;触发器&#34;没有找到类型&#39; ControlTemplate&#39; 你知道如何在通用应用程序中鼠标结束时更改图像的颜色吗? 谢谢你的帮助

1 个答案:

答案 0 :(得分:2)

您想要更改图像的颜色,我认为无法在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值,重写这些值并将其转回图像。并且Foreground控件没有Image属性。