D
D
Denis Mashanov2016-08-31 13:54:41
XAML
Denis Mashanov, 2016-08-31 13:54:41

How to programmatically specify an image in an Image?

Good afternoon everyone, evening. Please help. I can’t load an image into an Image programmatically, namely, there is such a code (style)

<Style x:Key="Base" TargetType="MenuItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}" x:Name = "customControl">
                        <Border Name="FastBorder" Height="22" Width="200">
                            <Grid>
                                <Image Name="Fast" Width="15" Height="15" HorizontalAlignment="Stretch" Margin="0,0,170,0" Source="{Binding IMGFast}"/>
                                <TextBlock Name="TextOP" Text="Быстрое удаление проекта" FontSize="12" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="30,4,0,0" Width="200" Height="25" Foreground="Lime" Background="{Binding BackColorFast}"/>
                            </Grid>
                        </Border>
                      </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

<Image Name="Fast" Width="15" Height="15" HorizontalAlignment="Stretch" Margin="0,0,170,0" Source="{Binding IMGFast}"/>

Source="{Binding IMGFast}"/>
I can’t programmatically add a picture here, initially I did it like this
DataContext = new MainWindow[]
            {
                new MainWindow
                {
                  IMGFast ="ok.png",
                  BackColorFast = "#25FFFFFF",
                  IMGFull ="",
                  BackColorFull = ""
                }
            };

everything worked, but there was a problem that when the form was closed, the process was in operation. What other ways are there to load from resources into an Image, which is written in the style?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Johnny Gat, 2016-08-31
@LoneRay

File "App.xaml":

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication1"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <BitmapImage x:Key="MyImageSource" UriSource="H:\Temp\WpfApplication1\WpfApplication1\bin\Debug\icon1.png" />
    </Application.Resources>
</Application>

File "MainWindow.xaml":
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Image Name="Fast" Width="15" Height="15" HorizontalAlignment="Stretch" Margin="0,0,170,0" Source="{DynamicResource MyImageSource}"/>
        <Button Name="button" Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    </Grid>
</Window>

File "MainWindow.xaml.cs":
using System;
using System.Windows;
using System.Windows.Media.Imaging;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Resources["MyImageSource"] =
                new BitmapImage(new Uri(@"H:\Temp\WpfApplication1\WpfApplication1\bin\Debug\icon2.png"));
        }
    }
}

Clicking the button changes the image on the form.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question