1

我尝试做的事情:我在背景中有一个个人资料列表,可与我的组合框一起使用。触发器是根据人的性别改变背景(bool Role.IsFemale)。当我在代码中处理 SelectionChangedEvent 时,我可以看到 Selectedvalue 是真还是假。我现在可以直接更改背景或更改用户控件本身可以侦听并在触发时更改背景的dependencyProperty。但是,我尝试只使用 xaml 来实现这一点,但是当我使用下面的代码时没有任何反应......

<UserControl ...
MinHeight="100" MinWidth="100" x:Name="Crtl">
<UserControl.Resources>
    <SolidColorBrush x:Key="windowBGBrush1" Color="Green"/>
    <SolidColorBrush x:Key="windowBGBrush2" Color="Red"/>
</UserControl.Resources> 
<UserControl.Style >
    <Style TargetType="{x:Type Control}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=SelectedValue, ElementName=cbProfiles}" Value="False">
                <Setter Property="Background" Value="{DynamicResource windowBGBrush1}"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=SelectedValue, ElementName=cbProfiles}" Value="True">
            <Setter Property="Background" Value="{DynamicResource windowBGBrush2}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Style>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="20*"/>
        <RowDefinition Height="80*" />
    </Grid.RowDefinitions>
    <ComboBox Name="cbProfiles" Grid.Row="0" ItemsSource="{Binding}" DisplayMemberPath="Role.RoleID" SelectedValuePath="Role.IsFemale"/>
    <StackPanel Grid.Row="1" x:Name="spFileInfo" DataContext="{Binding ElementName=cbProfiles, Path=SelectedItem}">
        <TextBlock>Selected:</TextBlock>
        <TextBox x:Name="tbFileFolder" Width="Auto" Height="Auto" Text="{Binding Path=Role.RoleID}"/>
    </StackPanel>
</Grid>

4

1 回答 1

0

我尝试了您的 Xaml,它对我来说很好,当我更改组合框时,它会更改背景。您的数据绑定可能有问题。我使用了以下数据结构

class Role
{
    public int RoleID { get; set; }
    public bool IsFemale { get; set; }

    public Role() {}
}

class Person
{
    public Role Role { get; set; }

    public Person() {}
}
于 2010-02-06T01:59:48.237 回答