Search code examples
c#androidwpfnine-patch

How to use 9-patch image in Wpf-app


I need to create and use 9-patch image ( *.9.png, like in android) in WPF-app!
Only one question - HOW?


Solution

  • WPF doesn't support 9-patch format out of the box that I'm aware of, so you're looking at a custom control - that said, I imagine it'd be one of the more "reasonable" controls to put together: really you'd be looking at setting up...well, to be simple, 9 different Images, each set Stretch to extents, all wrapped within a...oh, let's pick a DockPanel for the hell of it. The control template might look something like:

    (warning: slightly inebriated and free-handling this on a bloody phone, so likely not compile-able...in fact, now that I think of it, a Grid would probably be a wiser choice (3x3 rows cols, each cell with an image), but I shall persist in my original suggestion!)

    <DockPanel>
        <DockPanel DockPanel.Dock="Top">
            <Image DockPanel.Dock="Left" Source="{Binding LeftTopImage}"/>
            <Image DockPanel.Dock="Right" Source="{Binding RightTopImage}"/>
            <Image Source="{Binding CenterTopImage}"/>
        </DockPanel>
        <DockPanel DockPanel.Dock="Left">
            <Image Source="{Binding CenterLeftImage}"/>
        </DockPanel>
        <DockPanel DockPanel.Dock="Right">
            <Image Source="{Binding CenterRightImage}"/>
        </DockPanel>
        <DockPanel DockPanel.Dock="Bottom">
            <Image DockPanel.Dock="Left" Source="{Binding LeftBottomImage}"/>
            <Image DockPanel.Dock="Right" Source="{Binding RightBottomImage}"/>
            <Image Source="{Binding CenterBottomImage}"/>
        </DockPanel>
        <DockPanel>
            <Image Source="{Binding CenterImage}"/>
        </DockPanel>
    </DockPanel>