开发者

Silverlight style and binding

I encounter a problem with binding in my silverlight style.

This is my viewmodel :

public class MyObject
{
    public Uri TheUrl { get; set; }
    public string MyText { get; set; }
}

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public MyObject Object1 { get { return new MyObject { TheUrl = new Uri("test.png", UriKind.Relative), MyText = "Test1" }; } }
    pu开发者_开发百科blic MyObject Object2 { get { return new MyObject { TheUrl = new Uri("test.png", UriKind.Relative), MyText = "Test2" }; } }
}

And this is my xaml :

    <UserControl.Resources>
    <Style x:Key="TestStyle" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <StackPanel>
                        <TextBlock Text="{Binding MyObject.MyText}" />
                        <Image Source="{Binding MyObject.TheUrl}" />
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<UserControl.DataContext>
    <vm:ViewModel />
</UserControl.DataContext>

<StackPanel x:Name="LayoutRoot" Background="White">
    <Button Style="{StaticResource TestStyle}" Width="100" Height="100" Tag="{Binding Object1}" />
    <Button Style="{StaticResource TestStyle}" Width="100" Height="100" Tag="{Binding Object2}" />
</StackPanel>

I test many things in my style, but can't make the binding working.

Does someone have an idea ?

Thanks in advance for any help Best regards.

EDIT :

Changes to viewmodel :

public class MyObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private Uri _TheUrl;
    public Uri TheUrl
    {
        get { return _TheUrl; }
        set
        {
            _TheUrl = value;
            NotifyPropertyChanged("TheUrl");
        }
    }

    private string _MyText;
    public string MyText
    {
        get { return _MyText; }
        set
        {
            _MyText = value;
            NotifyPropertyChanged("MyText");
        }
    }
}

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private MyObject _Object1;
    public MyObject Object1
    {
        get { return _Object1; }
        set
        {
            _Object1 = value;
            NotifyPropertyChanged("Object1");
        }
    }

    private MyObject _Object2;
    public MyObject Object2
    {
        get { return _Object2; }
        set
        {
            _Object2 = value;
            NotifyPropertyChanged("Object2");
        }
    }

    public ViewModel()
    {
        Object1 = new MyObject {TheUrl = new Uri("test.png", UriKind.Relative), MyText = "Test1"};
        Object2 = new MyObject { TheUrl = new Uri("test.png", UriKind.Relative), MyText = "Test2" };
    }
}


it seems that using the DataContext instead of Tag on the Button is working


Your class doesn't correctly implement INotifyPropertyChanged interface.

Add this method:

private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

To your property setters add a call to NotifyPropertyChanged

private Uri _TheUrl;

public Uri TheUrl { 
    get { return _TheUrl;} 
    set { 
        _TheUrl = value; 
        NotifyPropertyChanged("TheUrl");
    } 
}

You should to the same with the Object1 and Object2 (basically with all objects you want to bind to)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜