0

我有一个 ListBox,每个 Item 都有一个绑定到某些数据的 ComboBox(和其他控件)。每当我更改其中一个组合框中的选定值时,它们都会在几分之一秒内获得一些红色边框。

这些组合框具有自定义样式,但其样式上没有红色边框。

调试后我发现如果我删除它,UI 会更新(显示的 SelectedValue 会发生变化),并且问题不会发生。

另一个问题是我只写了这个项目的视图。我不写 Model 和 ViewModel ,所以我不完全知道这个绑定中发生了什么。

有什么我可以做的吗?

编辑

这是组合框(位于 ListBox 的 ItemTemplate 中) -

<ComboBox Grid.Column="1" BorderThickness="0" Style="{StaticResource ResourceKey=settingsComboBox}" IsEnabled="{Binding Path=IsEnabled}" Height="15"  
    ItemsSource="{Binding Path=Options}" SelectedItem="{Binding Path=CurrentOption}" />  

这就是他的(相当长的)风格——

<Style x:Key="settingsComboBox" TargetType="ComboBox" >
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="Margin" Value="5,0,0,0" />
        <Setter Property="Template">
            <Setter.Value >
                <ControlTemplate>
                    <WrapPanel >
                        <Popup IsOpen="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" AllowsTransparency="True" 
                               PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.DirectionalNavigation="Contained" />
                        </Popup>
                        <ToggleButton Margin="0" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" VerticalAlignment="Center" HorizontalAlignment="Stretch" Width="90">
                            <ToggleButton.Style>
                                <Style TargetType="ToggleButton" >
                                    <Setter Property="Margin" Value="0,12,0,0" />
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate>
                                                <StackPanel Orientation="Horizontal" Background="Transparent" HorizontalAlignment="Stretch">
                                                    <ContentPresenter Content="{Binding RelativeSource={RelativeSource AncestorType=ComboBox}, Path=SelectionBoxItem}" TextBlock.Foreground="{Binding ElementName=ChannelTitle, Path=Foreground}" />
                                                    <Image Style="{StaticResource ResourceKey=comboBoxButtonImage}" />
                                                </StackPanel>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </ToggleButton.Style>
                        </ToggleButton>
                    </WrapPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemTemplate" >
            <Setter.Value>
                <DataTemplate>
                    <Border Width="80">
                        <Border.Style>
                            <Style TargetType="Border">
                                <Setter Property="BorderBrush" Value="{Binding ElementName=ChannelTitle, Path=Foreground}" />
                                <Setter Property="BorderThickness" Value="1.2, 1, 1.2, 0" />
                                <Setter Property="Background" Value="{Binding ElementName=frstGrid, Path=Background}" />
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True" >
                                        <Setter Property="Background" Value="{StaticResource ResourceKey=HoveredComboboxItem}" />
                                    </Trigger>

                                    <DataTrigger Binding="{Binding ElementName=frstGrid, Path=Background}" Value="White" >
                                        <Setter Property="BorderBrush" Value="#A6AAAE" />
                                    </DataTrigger>

                                    <DataTrigger Value="True">
                                        <DataTrigger.Binding>
                                            <MultiBinding Converter="{StaticResource ResourceKey=lastItemConverter}">
                                                <Binding RelativeSource="{RelativeSource AncestorType=ComboBoxItem}" />
                                                <Binding RelativeSource="{RelativeSource AncestorType={x:Type ComboBox}}" Path="Items" />
                                            </MultiBinding>
                                        </DataTrigger.Binding>
                                        <Setter Property="CornerRadius" Value="0,0,4,4" />
                                        <Setter Property="BorderThickness" Value="1.2,1,1.2,1.2" />
                                    </DataTrigger>
                                    <DataTrigger Value="True">
                                        <DataTrigger.Binding>
                                            <MultiBinding Converter="{StaticResource ResourceKey=firstItemConverter}">
                                                <Binding RelativeSource="{RelativeSource AncestorType=ComboBoxItem}" />
                                                <Binding RelativeSource="{RelativeSource AncestorType={x:Type ComboBox}}" Path="Items[0]" />
                                            </MultiBinding>
                                        </DataTrigger.Binding>
                                        <Setter Property="BorderThickness" Value="1.2,1.2,1.2,0" />
                                        <Setter Property="CornerRadius" Value="4,4,0,0" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Border.Style>
                        <DockPanel >
                            <TextBlock Text="{Binding}" DockPanel.Dock="Right" Margin="0,5" Foreground="{Binding ElementName=ChannelTitle, Path=Foreground}" HorizontalAlignment="Left" Padding="10,0,0,0" />
                        </DockPanel>
                    </Border>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

希望它会有所帮助。

4

0 回答 0