2012-05-17 27 views
0
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
    <Grid.Resources><DataTemplate x:Key="mDataTemplate"> 
    <Button BorderBrush="#FF767171" Margin="10,10,0,0" Click="button1_Click" IsEnabled="{Binding Enabled}"> 
     <Button.Content> 
     <Grid x:Name="ButtonGrid" Height="Auto" HorizontalAlignment="Left" erticalAlignment="Top"> 
      <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" />         
      </Grid.RowDefinitions> 
    <TextBlock Grid.Row="0" Height="Auto" HorizontalAlignment="Left" Text="{Binding TitleText}" VerticalAlignment="Top" Width="Auto" FontSize="30" /> 
    <TextBlock Grid.Row="1" Height="Auto" HorizontalAlignment="Left" Text="{Binding DetailText}" VerticalAlignment="Top" Margin="10,0,0,0" Width="Auto" FontSize="20" /> 
     </Grid> </Button.Content> </Button> </DataTemplate> </Grid.Resources> 

Silverlight的手機更新被綁定到列表框

我的代碼在DataTemplate中的一個按鈕。

public class AboutData 
    {   
     public string TitleText { get; set; } 
     public string DetailText { get; set; } 
     public bool Enabled { get; set; } 
    } 
} 

列表框的比如LoadEvent

ObservableCollection<AboutData> aCollection = new ObservableCollection<AboutData>(); 
aCollection.Add(new AboutData { TitleText="Title1", DetailText="Detail1", Enabled= true}); 
aCollection.Add(new AboutData { TitleText="Title2", DetailText="Detail2", Enabled= true}); 
aCollection.Add(new AboutData { TitleText="Title3", DetailText="Detail3", Enabled= true}); 

ContentPanel.DataContext = aCollection; 

當listBox1中(我的列表框)加載我做AboutData的一個ObservableCollection並將其分配給ControlPanel.DataContext

我想在禁用按鈕我單擊某個按鈕時的lisbox。 不知道如何。 幫助Apprciciated。

回答

0

您應該能夠設置

public void button1_Click(object sender, RoutedEvent e) 
{ 
    AboutData dataContext = (sender as Button).DataContext as AboutData; 
    dataContext.Enabled = false; 
} 

此外,還需要實現AboutData INotifyPropertyChanged接口的AboutData的Enabled屬性,以便結合作品(見this

public class AboutData : INotifyPropertyChanged 
{ 
    // boiler-plate 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    protected bool SetField<T>(ref T field, T value, string propertyName) 
    { 
     if (EqualityComparer<T>.Default.Equals(field, value)) return false; 
     field = value; 
     OnPropertyChanged(propertyName); 
     return true; 
    } 

    // props 
    private string _Enabled; 
    public string Enabled 
    { 
     get { return Enabled; } 
     set { SetField(ref _Enabled, value, "Enabled"); } 
    } 

    // etc. for TitleText, DetailText 
}