B
B
BadCats2017-06-27 10:49:50
WPF
BadCats, 2017-06-27 10:49:50

Using xaml markup extension in wpf?

If I create an instance of a certain class in c#
- the same OpenFileDialog - then how can I correctly refer to this class using a markup extension in xaml ?
OpenFileDialog myDialog = new OpenFileDialog();
<Image Source="{}" ></Image>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Johnny Gat, 2017-06-28
@BadCats

MainWindow.xaml

<Window x:Class="WpfApplication3.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:WpfApplication3"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Image Name="image" HorizontalAlignment="Left" Height="282" Margin="18,18,0,0" VerticalAlignment="Top" Width="394" Source="{Binding MySource}"/>
        <Button Name="button" Content="Button" HorizontalAlignment="Left" Margin="432,18,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    </Grid>
</Window>

using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Windows;

namespace WpfApplication3
{
    public partial class MainWindow : Window
    {
        public MyClass Class;

        public MainWindow()
        {
            InitializeComponent();

            Class = new MyClass();
            image.DataContext = Class;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog myDialog = new OpenFileDialog();
            if (myDialog.ShowDialog().Value)
                Class.MySource = myDialog.FileName;
        }
    }

    public class MyClass : INotifyPropertyChanged
    {
        private String mySource;
        public String MySource
        {
            get
            {
                return mySource;
            }
            set
            {
                mySource = value;
                OnPropertyChanged("MySource");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

S
Smilley, 2017-06-27
@Smilley

We need to create a property that will store the URI of the picture selected in myDialog. The class where the property is stored must implement the INotifyPropertyChanged interface. In Source you need to write the following: Source={Binding MyProperty}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question