0

Как биндит Image? Поля типа string и bool отображаются, а Image не отображается. C#,WPF

<av:Window x:Class="MessengerVK.MessageManager"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       xmlns:viewModel="clr-namespace:MessengerVK.ViewModel"
       Title="MessageManager"
       Width="640"
       Height="480"
       mc:Ignorable="d">
<Window.DataContext>
    <viewModel:MessageManagerViewModel/>
</Window.DataContext>
<av:Grid ShowGridLines="True">
    <av:Grid.ColumnDefinitions>
        <av:ColumnDefinition Width="2*" />
        <av:ColumnDefinition Width="4*" />
        <av:ColumnDefinition Width="1*" />
    </av:Grid.ColumnDefinitions>
    <av:Grid.RowDefinitions>
        <av:RowDefinition Height="1*" />
        <av:RowDefinition Height="2*" />
        <av:RowDefinition Height="6*" />
        <av:RowDefinition Height="2*" />
    </av:Grid.RowDefinitions>        
    <ListView Grid.Column="0" Grid.Row="2" Grid.RowSpan="2">
        <ListViewItem>  
           <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
               <TextBlock Grid.Column="0" Width="70" Text="{Binding Path=Template.Name}"/>
               <TextBlock Grid.Column="1" Width="50" Text="{Binding Path=Template.Online}"/>
               <Image Grid.Column="2" Width="70" Height="70" Source="{Binding Path=Template.Avatar}"/>
             </Grid>
        </ListViewItem>
    </ListView>       
    </Grid>
</av:Grid>

public class Friend
{
    public string Name { get; set; }
    public bool Online { get; set; }
    public Image Avatar { get; set; }
}


public class MessageManagerViewModel:ViewModelBase,INotifyPropertyChanged
{

    private Friend template;
    public Friend Template
    {
        get
        {
            return template;
        }

        set
        {
            template = value;
        }
    }

    public MessageManagerViewModel()
    {

        Template.Name = "Azizxon";
        Template= Image.FromFile(@"C:\Users\Lenovo\Desktop\a.png");


    }


}
1
  • А зачем вам namespace av?
    – VladD
    11 окт 2015 в 17:39

1 ответ 1

1

Для приложений на WPF пользуйтесь в VM не System.Drawing.Image, а System.Windows.Media.BitmapImage.

Дело в том, что вы пользуетесь контролом System.Windows.Controls.Image, и привязываетесь к его свойству Source. Это свойство имеет тип ImageSource, а System.Drawing.Image не является наследником ImageSource.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.