【问题标题】:WPF trigger for selected items选定项目的 WPF 触发器
【发布时间】:2012-07-09 17:58:11
【问题描述】:

我正在使用 MVVM 轻框架。在我的一个页面上,我有一个列表框,其中包含一个绑定列表和一个绑定的选定项目。我有一个样式应该突出显示该列表框中的选定项目。但是,直到我物理单击该项目才会突出显示(刚绑定时它不会突出显示该项目)。

XAML:

<UserControl.Resources>
        <hartvm:ViewLocator x:Key="HartvilleLocator" d:IsDataSource="True" />
        <DataTemplate DataType="{x:Type Quotes:QuotePetSummaryItem}">
            <Views:QuoteSummaryItem DataContext="{Binding}" />
        </DataTemplate>
        <Style x:Key="ListboxItemStyle" TargetType="{x:Type ListBoxItem}">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
            </Style.Resources>
            <Setter Property="BorderBrush" Value="Red"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="False">
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Opacity" Value="40"/>
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="Opacity" Value="100"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    <UserControl.DataContext>
        <Binding Source="{StaticResource HartvilleLocator}" Path="QuoteSummary" />
    </UserControl.DataContext>
    <Border>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Border BorderBrush="Black" BorderThickness="0">
                <TextBlock TextWrapping="Wrap" Text="Quote Summary" Margin="5,0,0,0" FontSize="21.333" Foreground="{DynamicResource ControlHeaderBrush}" FontFamily="Verdana"/>
            </Border>
            <ScrollViewer d:LayoutOverrides="Height" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="1" Margin="10,10,5,10">
                <telerik:RadBusyIndicator IsBusy="{Binding IsProcessing}">
<ListBox ItemsSource="{Binding DefaultList}" SelectedItem="{Binding SelectedDefault}" Background="{x:Null}" ItemContainerStyle="{StaticResource ListboxItemStyle}"> 

                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                                <WrapPanel Orientation="Horizontal" />
                            </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    </ListBox>
                </telerik:RadBusyIndicator>
            </ScrollViewer>
        </Grid>
    </Border>
</UserControl>

代码(以编程方式选择列表框中的项目):

public QuotePetSummaryItem SelectedDefault { get; set; }

private void SelectDefault()
        {
            if (DefaultList== null || DefaultList.First().Pet == null) return;

            SelectedDefault = DefaultList.First();

            MessengerService.Send(SelectionMessage.Create(SelectedDefault.SubDefault));
        }

正在发送的消息并不重要,并且在另一个页面上做了一些其他工作。 SelectedDefault 属性是本示例中唯一使用的属性。

有人知道我需要做什么才能使其正常工作吗?

【问题讨论】:

  • SelectedDefault = DefaultList.First(); 行放一个断点。它会被击中吗?请发布SelectedDefault的代码。

标签: c# wpf xaml mvvm listbox


【解决方案1】:

看起来绑定(因此您的视图)不知道所选项目属性已更改。

在 SelectedDefault 的设置器中,您需要使用 INotifyPropertyChanged 接口创建某种通知。

我只是快速浏览了 MVVM 灯光框架并通过示例判断,如果您的视图模型从 ViewModelBase 继承,则使用字段支持的属性并调用 RaisePropertyChanged:

private QuotePetSummaryItem _selectedDefault; public QuotePetSummaryItem SelectedDefault { get { return _selectedDefault; } set { _selectedDefault = value; RaisePropertyChanged("SelectedDefault"); } }

【讨论】:

    【解决方案2】:

    首先你必须将所选项目的绑定模式设置为双向

    <ListBox ItemsSource="{Binding DefaultList}" SelectedItem="{Binding SelectedDefault, Mode=TwoWay}" Background="{x:Null}" ItemContainerStyle="{StaticResource ListboxItemStyle}"> 
    

    在您的视图模型中,您必须实施 INotifyPropertyChanged 并正确提升它。看看 Rock Counters 的回答。

    如果这一切都不起作用,请检查您的 vs 输出窗口是否存在绑定错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      • 2010-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多