简体   繁体   中英

WPF custom control doesn't bind

<UserControl
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"
mc:Ignorable="d"
x:Class="Menupedia.MiniRestaurantViewer"
x:Name="UserControl" Width="80" Height="100">

<Grid x:Name="LayoutRoot">
    <Label x:Name="label_Name" Content="{Binding _name, Mode=OneWay}" HorizontalAlignment="Stretch" Width="80" FontFamily="Public Enemy NF" FontSize="14.667" Foreground="#FFEF7B54" Margin="0" Height="20" VerticalAlignment="Bottom"/>
    <Image x:Name="image_Logo" Source="{Binding _logo, Mode=OneWay}" HorizontalAlignment="Left" Width="80" Height="80" VerticalAlignment="Top"/>
    <Border BorderBrush="#FFF15A28" BorderThickness="1" Height="80" CornerRadius="2" VerticalAlignment="Top" Width="80" HorizontalAlignment="Left"/>
</Grid>

public partial class MiniRestaurantViewer : UserControl
{
    public int _id {get{return id;}}
    public string _name {get{return name;}}
    public ImageSource _logo {get{return logo;}}

    public MiniRestaurantViewer(int id, string name,byte[] logo)
    {
        this.id = id;
        this.name = name;
        this.logo = ByteArrayToImageSource(logo);
        this.InitializeComponent();
    }

    private int id;
    private string name;
    private ImageSource logo;

    private ImageSource ByteArrayToImageSource(byte[] data)
    {
        BitmapImage image = null;
        if (null != data)
        {
            image = new BitmapImage();
            image.BeginInit();
            image.StreamSource = new System.IO.MemoryStream(data);
            image.EndInit();
        }
        return image;
    }

    public MiniRestaurantViewer()
    {
        this.InitializeComponent();
    }
}

that is my custom control. i want to do this

ListBox.Items.Add(new MiniRestaurantViewer(1,"test",null));

when i do it i see the UI element but it's empty (binding didn't work). Through the watch though i find that the public properties has values.. i don't know how to make it work and i have been tryin since 3 days please help me. :(

You can do this this.DataContext = this; to make your code works but you are far far away from the best practice of wpf which means using MVVM try to read this first as it can be a good start for a begineer

 public MiniRestaurantViewer(int id, string name,byte[] logo)
    {
        this.id = id;
        this.name = name;
        this.logo = ByteArrayToImageSource(logo);
        this.InitializeComponent();
        this.DataContext = this;

    }

You need to set DataContext to itself if properties lies in code behind.

<UserControl x:Class="Menupedia.MiniRestaurantViewer"
             x:Name="UserControl" Width="80" Height="100"
             DataContext="{Binding RelativeSource={RelativeSource Self}}">

First of all, I think you need to use two way bindings to be able to make changes both way - from your view to model and from model to view. I did not see in your control Listbox, so probably it's from your mainWindow. In this case your have two oportunities - or to set datacontext of the mainWindow, like in an answers below, or set ListBox.Datacontext. Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM