将模板应用于按钮单击事件上的列表框

本文关键字:列表 事件 单击 应用于 按钮 | 更新日期: 2023-09-27 18:11:11

我想在点击"btnDetails"按钮后将"detail"模板应用到列表框。

<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>

我已经尝试了上面的代码,但它不工作。

将模板应用于按钮单击事件上的列表框

下面是一个示例,当您按下按钮

时将修改项模板

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>

我把你的原始控件包装在内容控件的控件模板中,以实现对IsPressed属性的控制

如果你想要切换按钮那么这里是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>
在上面的xaml中,我使用了切换按钮来代替常规按钮,以保持
的详细信息。

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