在弹出窗口中重置绑定

时间:2014-05-21 16:38:04

标签: wpf wpf-controls

我正在尝试以编程方式在自定义可编辑多选组合框的弹出窗口中选择项目。 我已经重新设计了组合框popup以处理所选项目:

<Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Right">
            <themes:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}">
                <Border x:Name="dropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                    <ScrollViewer x:Name="DropDownScrollViewer">
                        <Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
                            <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                <Rectangle x:Name="opaqueRect" Fill="{Binding Background, ElementName=dropDownBorder}" Height="{Binding ActualHeight, ElementName=dropDownBorder}" Width="{Binding ActualWidth, ElementName=dropDownBorder}"/>
                            </Canvas>
                            <ListBox x:Name="ListBox" SelectionMode="Multiple" ItemsSource="{TemplateBinding ItemsSource}" 
                                     KeyboardNavigation.DirectionalNavigation="Contained" Focusable="False"
                                     SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Name}"/>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                                <ListBox.ItemContainerStyle>
                                    <Style TargetType="{x:Type ListBoxItem}">
                                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                                    </Style>
                                </ListBox.ItemContainerStyle>
                            </ListBox>
                        </Grid>
                    </ScrollViewer>
                </Border>
            </themes:SystemDropShadowChrome>
        </Popup>

我可以成功完成以下任务:

    if (_temporarySelectedItem != null)
        {
            var item = _model.OptionItems.Single(x => x.Name.Equals(_temporarySelectedItem.Name));
            item.IsSelected = true;
            ItemsSource = _model.OptionItems;
        }

然后选择了正确的项目,但是第一次没有进行任何更改。换一种说法。如果我在弹出窗口中有项目a,b和c,并且我将a设置为选中,则弹出窗口将其显示为已选中,但如果我将b设置为已选中,则不会观察到任何更改(此时检查ItemsSource显示实际上该项确实将其IsSelected设置为true。

有关如何让列表框重绘的任何想法?

0 个答案:

没有答案