Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当面板中的元素具有焦点时,更改WPF StackPanel背景色_Wpf_Background_Focus_Stackpanel - Fatal编程技术网

当面板中的元素具有焦点时,更改WPF StackPanel背景色

当面板中的元素具有焦点时,更改WPF StackPanel背景色,wpf,background,focus,stackpanel,Wpf,Background,Focus,Stackpanel,如果StackPanel中有一组控件,当StackPanel中的任何控件获得焦点时,是否有一种通用的方法来更改StackPanel的背景?(显然,当StackPanel中没有控件具有焦点时,将背景切换回原来的位置)。下面的代码对我很有用,但是如果有一种通用的方法来完成这项任务,而不是在我的页面中列出每个StackPanel中的每个控件,那就更好了 谢谢 <StackPanel Margin="5"> <StackPanel.Style> <Style

如果StackPanel中有一组控件,当StackPanel中的任何控件获得焦点时,是否有一种通用的方法来更改StackPanel的背景?(显然,当StackPanel中没有控件具有焦点时,将背景切换回原来的位置)。下面的代码对我很有用,但是如果有一种通用的方法来完成这项任务,而不是在我的页面中列出每个StackPanel中的每个控件,那就更好了

谢谢

<StackPanel Margin="5">
    <StackPanel.Style>
    <Style TargetType="{x:Type StackPanel}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsFocused, ElementName=chkOccupiedByMortgagor}" Value="true">
                <Setter Property="Background" Value="Gray" />
                <Setter Property="Opacity" Value=".5" />
            </DataTrigger>
            <DataTrigger Binding="{Binding IsFocused, ElementName=chkOccupiedByNewOwner}" Value="true">
                <Setter Property="Background" Value="Gray" />
                <Setter Property="Opacity" Value=".5" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</StackPanel.Style>
<CheckBox Margin="2" x:Name="chkOccupiedByMortgagor">Mortgagor</CheckBox>
<CheckBox Margin="2" x:Name="chkOccupiedByNewOwner">New Owner</CheckBox>
<CheckBox Margin="2" x:Name="chkOccupiedByTenant">Tenant</CheckBox>
<CheckBox Margin="2" x:Name="chkOccupiedByUnknownOccupant">Unknown Occupant</CheckBox>
</StackPanel> 

抵押人
新主人
房客
未知居住者

是。你可以做到。只需对触发器使用属性,如下所示:

<StackPanel Margin="5">
    <StackPanel.Style>
        <Style TargetType="{x:Type StackPanel}">
            <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsKeyboardFocusWithin}" Value="True">
                    <Setter Property="Background" Value="Gray" />
                    <Setter Property="Opacity" Value=".5" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
    <CheckBox Margin="2">Mortgagor</CheckBox>
    <CheckBox Margin="2">New Owner</CheckBox>
    <CheckBox Margin="2">Tenant</CheckBox>
    <CheckBox Margin="2">Unknown Occupant</CheckBox>
</StackPanel>

酷!正是我要找的!非常感谢!
<StackPanel Margin="5" x:Name="stackPanel">
    ...
                <DataTrigger Binding="{Binding ElementName=stackPanel, Path=IsKeyboardFocusWithin}" Value="True">
    ...