2011-05-23 126 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