【问题标题】:In a Listbox change the color of textbox when checking a checkbox c# wpf在列表框中选中复选框时更改文本框的颜色 c# wpf
【发布时间】:2021-05-24 17:49:59
【问题描述】:

我有一个包含 3 个按钮、2 个文本框和每个元素中的复选框的列表框。

当我选中复选框时,我希望按钮和 txtboxes 更改属性(颜色/或其他)。

希望有人可以帮助我:)

谢谢你的回答...

        <!-- LISTBOX -->
        <ListBox Name="lstBoxInfoWindow" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Foreground="Black" ScrollViewer.VerticalScrollBarVisibility="Visible" Margin="5,1.2,4.6,-0.4" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="FontWeight" Value="Bold"/>
                            <Setter Property="Background" Value="Transparent"/>
                            <Setter Property="Foreground" Value="Black"/>
                        </Trigger>
                        <Trigger Property=""
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                        <Button Name="btnPackageVersion" Content ="Package version" Click="btnPackageVersion_Click" HorizontalAlignment="Right" Margin="10,0,10,0">

                        </Button>
                        <Button Name="btnFilterPath" Content ="Filter path" Click="btnFilterpath_Click" HorizontalAlignment="Right"/>
                        <TextBlock Name="txtBlkPath" Text="{Binding path, Mode=TwoWay}" FontSize="10" Width="500" Height="20" Margin="10,0,10,0"/>

                        <Button Name="btnFilterfile" Content="Filter file" Click="btnFilterfile_Click" HorizontalAlignment="Right"/>
                        <TextBlock Name="txtBlkFile" Text="{Binding file, Mode=TwoWay}" FontSize="10" Width="150" Height="20" Margin="10,0,10,0"/>

                        <CheckBox Name="chkBxKill" Content="Kill" VerticalAlignment="Center" HorizontalAlignment="Right" IsChecked="{Binding keepKill.kill, Mode=TwoWay}" Margin="5,0,5,0" Checked="chkBxKill_Checked"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListBox>

    </Grid>
</Window>

【问题讨论】:

    标签: c# wpf xaml listbox listboxitem


    【解决方案1】:

    使用DataTemplate 中定义的DataTrigger

    以下示例在选中CheckBox 时将按钮的Backgound 设置为红色:

    <DataTemplate>
      <StackPanel Orientation="Horizontal"
                  HorizontalAlignment="Right">
        ...
    
        <Button Name="btnFilterfile" />    
        <CheckBox Name="chkBxKill" />
      </StackPanel>
    
      <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding ElementName=chkBxKill, Path=IsChecked}" Value="True">
          <Setter TargetName="btnFilterfile" Property="Background" Value="Red" />
        </DataTrigger>
      </DataTemplate.Triggers>
    </DataTemplate>
    

    【讨论】:

    • TY 为您解答。首先我需要说,我是一个完全的新手,所以我可能会说一些愚蠢的话!。我已经实现了您的建议,并且按钮上似乎没有“背景”属性。有一个叫做 TextElement.FontWeight 的东西,我给它的值是 Extralight。但它没有显示在列表框中。
    • &lt;DataTemplate.Triggers&gt; &lt;DataTrigger Binding="{Binding ElementName=chkBxKill, Path=IsChecked}"&gt; &lt;Setter TargetName="btnFilterfile" Property="TextElement.FontWeight" Value="ExtraLight"/&gt;
    • 确保您使用来自正确命名空间的正确按钮。它必须是System.Windows.Controls.Button,然后您将获得 Background 属性。 TextElement.FontWeight 不适用于 Button。不确定您使用的是什么命名空间(Windows.UI.XAML - UWP?)
    • 查看代码后,我发现DataTrigger 上缺少Value="True" 属性。我已经添加了它。现在它应该工作了。我在编辑框中写了这段代码(没有编译器支持),很抱歉。
    • 哦,我不只是高兴得到帮助 :) TY 成功了 :) 好人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 2020-09-21
    • 2015-05-17
    相关资源
    最近更新 更多