XAML SVG เปลี่ยนสีเมื่อรันไทม์

เรามีชุด SVG ที่จัดเก็บไว้ในพจนานุกรมทรัพยากร

ตัวอย่าง:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DrawingImage x:Key="Bell">
        <DrawingImage.Drawing>
            <DrawingGroup Opacity="1">
                <DrawingGroup.Children>
                    <DrawingGroup Opacity="1">
                        <DrawingGroup.Children>
                            <DrawingGroup Opacity="1">
                                <DrawingGroup.Children>
                                    <GeometryDrawing Brush="#FF000000" Pen="{x:Null}">
                                        <GeometryDrawing.Geometry>
                                            <PathGeometry FillRule="Nonzero" Figures="........." />
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>
                                </DrawingGroup.Children>
                            </DrawingGroup>
                        </DrawingGroup.Children>
                    </DrawingGroup>
                </DrawingGroup.Children>
            </DrawingGroup>
        </DrawingImage.Drawing>
    </DrawingImage>
</ResourceDictionary>

หากคุณสังเกตเห็นว่า Geometry Drawing Brush ถูกตั้งค่าเป็น #ff000000 (สีดำ) ปัญหาที่เราเผชิญคือการอนุญาตให้มุมมองแสดง SVG นี้และกำหนดสีขณะรันไทม์ (ผ่านการผูก)

หน้าต่างของเรา (มุมมอง) มีพจนานุกรมทรัพยากรพร้อมไอคอนภายใน Window.Resources

เรากำลังมองหาวิธีแก้ปัญหาดังนี้:

<Image Source="{StaticResource Bell}" Fill="#FF884422"/>

person Sean Hobbs    schedule 08.04.2015    source แหล่งที่มา
comment
ดูเหมือนจะยุ่งยากมาก โดยส่วนตัวแล้วฉันเพิ่ง ส่งออก เป็น XAML และเปลี่ยนให้เป็น ContentControl ที่เป็นเทมเพลต โดยที่ฉันสามารถส่งต่อทุกสิ่งที่เป็นคุณสมบัติการพึ่งพาไปยังองค์ประกอบลูกในซับเดียวและจัดเก็บเทมเพลตในพจนานุกรมทรัพยากร   -  person Chris W.    schedule 08.04.2015
comment
ดูเหมือนว่าเส้นทางที่ฉันจะไป คุณมีตัวอย่างที่คุณสามารถแบ่งปันได้หรือไม่?   -  person Sean Hobbs    schedule 09.04.2015
comment
ใช่ ฉันมีการประชุมจะเริ่มในอีกสักครู่ แต่จะกลับมาที่นี้หลังอาหารกลางวัน และโทรหาคุณทันทีที่มีเวลาสักครู่   -  person Chris W.    schedule 09.04.2015
comment
ขอบคุณ! คุณผู้ชาย!   -  person Sean Hobbs    schedule 09.04.2015
comment
คริส ในเทมเพลตของคุณ คุณใส่อะไรเข้าไปข้างใน? PathGeometry หรือเพียงแค่ค่า Path?   -  person Sean Hobbs    schedule 09.04.2015
comment
คุณสามารถโยนสิ่งที่คุณชอบลงไปได้ ฉันลืมไปว่าฉันตอบคำถามที่คล้ายกัน ที่นี่ เพื่อให้คุณมีข้อมูลอ้างอิง จากนั้นคุณสามารถตั้งค่า setter สำหรับคุณสมบัติการพึ่งพาที่คุณต้องการเข้าถึงในเทมเพลตได้ตราบใดที่คุณใช้ {TemplateBinding blah} ซึ่งจะอนุญาตให้คุณทำ อย่างเช่น ‹ContentControl Fill=Red Stroke=Blue/› ฯลฯ เป็นต้น   -  person Chris W.    schedule 09.04.2015
comment
เนื่องจากข้อจำกัดของความคิดเห็น ฉันจึงต้องแบ่งคำตอบออกเป็นสองส่วน ฉันลองทำสิ่งต่อไปนี้ (ลบ xaml บางส่วนออก) แต่ {TemplateBinding Background} ดูเหมือนจะไม่ทำงาน   -  person Sean Hobbs    schedule 09.04.2015
comment
‹Style x:Key=SomeStyle TargetType=ContentControl› ‹Setter Property=Template› ‹Setter.Value› ‹ControlTemplate TargetType=ContentControl› ‹Image› ‹Image.Source› ‹ DrawingImage› .... ‹Geometry Drawing Brush={พื้นหลัง TemplateBinding }/› .... ‹/ DrawingImage› ‹/ Image.Source› ‹/ Image› ‹/ ControlTemplate› ‹/ Setter.Value› ‹/ Setter› ‹/ Style›   -  person Sean Hobbs    schedule 09.04.2015
comment
คุณจะเพิ่มคุณสมบัติการพึ่งพาใหม่ให้กับการควบคุมเนื้อหาอย่างไร คุณกำลังสร้างการควบคุมเนื้อหาแบบกำหนดเองที่สืบทอดและผนวกคุณสมบัติใหม่หรือไม่ จะทำอย่างไรถ้าฉันต้องการผูกหลายสีเข้ากับไอคอนนี้?   -  person Sean Hobbs    schedule 09.04.2015
comment
ให้เราสนทนาต่อในการแชท   -  person Sean Hobbs    schedule 09.04.2015
comment
รับทราบ คุณสามารถลบได้ ฉันจะส่งอีเมลถึงสิ่งที่ฉันเขียนไว้ในแชท   -  person Sean Hobbs    schedule 09.04.2015


คำตอบ (3)


ด้วยการสร้างคำตอบที่ไม่ทำงานของเปาโล ฉันสามารถแก้ไขปัญหานี้ได้

คลาส MyImage:

Public Class MyImage
    Inherits System.Windows.Controls.Image

    Public Property Color As System.Windows.Media.SolidColorBrush

End Class

ภายในพจนานุกรมทรัพยากร ให้กำหนด DrawingImage ให้กับตัวตั้งค่าแหล่งที่มาของสไตล์ MyImage:

<Style TargetType="{x:Type local:MyImage}" x:Key="Bell">
        <Setter Property="Source">
            <Setter.Value>
                <DrawingImage>
                    <DrawingImage.Drawing>
                        <DrawingGroup Opacity="1">
                            <DrawingGroup.Children>
                                <DrawingGroup Opacity="1">
                                    <DrawingGroup.Children>
                                        <DrawingGroup Opacity="1">
                                            <DrawingGroup.Children>
                                                <GeometryDrawing
                                                    Brush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyImage}}, Path=Color}"
                                                    Pen="{x:Null}" />
                                            </DrawingGroup.Children>
                                        </DrawingGroup>
                                    </DrawingGroup.Children>
                                </DrawingGroup>
                            </DrawingGroup.Children>
                        </DrawingGroup>
                    </DrawingImage.Drawing>
                </DrawingImage>
            </Setter.Value>
        </Setter>
</Style>

ภายในไฟล์ XAML ของหน้าต่าง:

<Window
    ...
    xmlns:local="clr-namespace:AppNameHere">
    <Window.Resources>
        <ResourceDictionary Source="DictionaryName.xaml" />
    </Window.Resources>
    ....
    <Grid Background="Black">
        <local:MyImage Color="Chartreuse" Width="30" Height="30" Style="{StaticResource Bell}" />
    </Grid>
    ...
</Window>

ผลลัพธ์ที่ได้คือ https://i.stack.imgur.com/7JNyH.png

person Brandon Hood    schedule 10.04.2015

ดูเครื่องมือนี้: https://github.com/BerndK/SvgToXaml

มันสามารถแปลง svg ทั้งหมดเป็น xaml เดียวโดยอัตโนมัติ สีจะถูกแยกออกจากกันและสามารถตั้งค่าให้กับทุกภาพในคราวเดียวหรือเพียงภาพเดียวก็ได้ รวมโค้ดตัวอย่างสำหรับการเปลี่ยนสีระหว่างรันไทม์ด้วย

เครื่องมือนี้ยังเป็นเบราว์เซอร์ svg โปรแกรมดู ...

person BerndK    schedule 07.05.2015
comment
@Joseph: ไลบรารี่ได้รับการออกแบบมาเพื่อแปลง SVG เป็นวัตถุ .NET Path อันนี้ออกแบบมาโดยไม่ได้ลงสี ดังนั้นคุณจึงสามารถ (และต้อง) กำหนดสีที่ต้องการได้ เหมาะสำหรับไอคอนสีเดียวที่ใช้ในการออกแบบสมัยใหม่ หากคุณต้องการไอคอนหลายสี คุณสามารถรวมหลายเส้นทางได้ (ดังที่แสดงในตัวอย่าง SvgToXaml) แต่ฉันจะพิจารณาใช้ไฟล์ png การทำเช่นนี้อาจทำให้คุณสูญเสียข้อดีของเส้นทาง/เรขาคณิต: ปรับขนาดได้โดยไม่สูญเสียคุณภาพ - person BerndK; 22.06.2020

DrawingImage จะได้รับบริบทข้อมูลเดียวกันกับหน้าต่าง ดังนั้นคุณจึงสามารถผูกสีเข้ากับคุณสมบัติบนโมเดลมุมมองของหน้าต่างได้

พจนานุกรม1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DrawingImage x:Key="Bell">
        <DrawingImage.Drawing>
            <DrawingGroup Opacity="1">
                <DrawingGroup.Children>
                    <DrawingGroup Opacity="1">
                        <DrawingGroup.Children>
                            <DrawingGroup Opacity="1">
                                <DrawingGroup.Children>
                                    <GeometryDrawing Brush="{Binding IconColor}" Pen="{x:Null}">
                                        <GeometryDrawing.Geometry>
                                            <PathGeometry FillRule="Nonzero" Figures="........." />
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>
                                </DrawingGroup.Children>
                            </DrawingGroup>
                        </DrawingGroup.Children>
                    </DrawingGroup>
                </DrawingGroup.Children>
            </DrawingGroup>
        </DrawingImage.Drawing>
    </DrawingImage>
</ResourceDictionary>

MyTestWindow.xaml

<Window x:Class="MyTestWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">        
    <Window.Resources>
        <ResourceDictionary Source="Dictionary1.xaml" />
    </Window.Resources>
    <Grid>
        <Image Source="{StaticResource Bell}" />
    </Grid>
</Window>

จากนั้นโมเดลมุมมองจะต้องมีคุณสมบัติ IconColor

person MHollis    schedule 08.04.2015