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>

My code.

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

Loadevent of listbox

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;

When listBox1 (my listbox) is loaded I make an ObservableCollection of AboutData and assign it to ControlPanel.DataContext

I Want to disable the button in my lisbox on click of certain button. Don't know how. Help Apprciciated.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

You should be able to set the Enabled property of the AboutData

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

In addition, you need to implement INotifyPropertyChanged on AboutData so that binding works (see 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
}
Community
  • 1
  • 1
Chui Tey
  • 5,436
  • 2
  • 35
  • 44