Reputation: 769
I want to apply "detail" template to list box after "btnDetails" button click.
<Window>
<Window.Resources>
<DataTemplate x:Key="detail">
<TextBlock Text="ABC" Background="Yellow"/>
</DataTemplate>
<Style x:Key="MyItemStyle" TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="Button.IsPressed" Value="True">
<Setter Property="Template" Value="{DynamicResource detail}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<ListBox Name="lbDetails" ItemContainerStyle="{Binding MyItemStyle}"></ListBox>
<Button Name="btnDetails">Details</Button>
</StackPanel>
</Window>
I have tried with above code but its not working.
Upvotes: 0
Views: 603
Reputation: 13679
here is a sample which will modify the item template when you press the button
Using Button (Push to on switch)
<ContentControl>
<ContentControl.Resources>
<DataTemplate x:Key="detail">
<TextBlock Text="ABC"
Background="Yellow" />
</DataTemplate>
</ContentControl.Resources>
<ContentControl.Template>
<ControlTemplate>
<StackPanel>
<ListBox Name="lbDetails">
dummy item
</ListBox>
<Button Name="btnDetails">Details</Button>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsPressed"
Value="True"
SourceName="btnDetails">
<Setter TargetName="lbDetails"
Property="ItemTemplate"
Value="{StaticResource detail}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
I did wrap your original controls in a content control's control template for achieving control over the IsPressed property
if you want a toggle kind of button then here is the xaml
Using Toggle (on off switch)
<ContentControl>
<ContentControl.Resources>
<DataTemplate x:Key="detail">
<TextBlock Text="ABC"
Background="Yellow" />
</DataTemplate>
</ContentControl.Resources>
<ContentControl.Template>
<ControlTemplate>
<StackPanel>
<ListBox Name="lbDetails">
dummy item
</ListBox>
<ToggleButton Name="btnDetails">Details</ToggleButton>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="ToggleButton.IsChecked"
Value="True"
SourceName="btnDetails">
<Setter TargetName="lbDetails"
Property="ItemTemplate"
Value="{StaticResource detail}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
in above xaml I have used toggle button in place of a regular button to keep the details on
Upvotes: 2
Reputation: 8654
Xaml
<Window.Resources>
<DataTemplate x:Key="detail">
<TextBlock Text="{Binding text}" Background="{Binding bg}"/>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ListBox Name="lbDetails" ItemTemplate="{StaticResource detail}"/>
<Button Name="btnDetails" Height="35" Content="Add" Click="btnDetails_Click_1"/>
</StackPanel>
c#
public partial class MainWindow : Window
{
ObservableCollection<listboxData> lst = new ObservableCollection<listboxData>();
public MainWindow()
{
InitializeComponent();
lbDetails.ItemsSource = lst;
}
private void btnDetails_Click_1(object sender, RoutedEventArgs e)
{
lst.Add(new listboxData("Textblock" + lst.Count, new SolidColorBrush(Colors.YellowGreen)));
}
}
public class listboxData
{
public string text { get; set; }
public SolidColorBrush bg { get; set; }
public listboxData(string text, SolidColorBrush bg)
{
this.text = text;
this.bg = bg;
}
}
Upvotes: 1