奇怪的绑定问题

时间:2013-10-17 09:12:14

标签: wpf binding customization

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

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

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

另一个问题是我只写了这个项目的视图。我没有编写模型和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>

希望它会有所帮助。

0 个答案:

没有答案