2011-05-23 59 views
0

我在Silverlight样式中遇到绑定问题。Silverlight样式和绑定

这是我的视图模型:

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" }; } } 
    public MyObject Object2 { get { return new MyObject { TheUrl = new Uri("test.png", UriKind.Relative), MyText = "Test2" }; } } 
} 

这是我的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> 

我在风格测试很多东西,但不能使结合工作。

有人有想法吗?

在此先感谢您的帮助 此致敬意。

编辑:

更改视图模型:

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" }; 
    } 
} 

回答

1

似乎使用DataContext代替按钮上的标签正在工作

0

你的类不能正确实现INotifyPropertyChanged接口。

添加这个方法:

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

你的财产制定者NotifyPropertyChanged

private Uri _TheUrl; 

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

你应该添加一个调用相同与您要绑定的所有对象的Object1Object2(基本上到)

+0

我以为我的viewmodel本来就足够用于这个演示了。顺便说一句,我已经改变了viewmodel(见编辑),它也不工作 – Tim 2011-05-23 08:52:00