在WPF应用程序中仅更改属性一次

时间:2015-08-27 13:22:06

标签: c# wpf

我如何unboldListBox选择文字,并在删除选择后保持这种方式?在我的代码中,删除选择后粗体返回。

我已经尝试使用Mode = "OneTime",但它仅适用于一个项目。

<ListBox x:Name="list" ItemsSource="{Binding EmailsCollection}" SelectedItem="{Binding SelectedItem}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <Label Content="{Binding Sender}" Style="{StaticResource Sender}" Name="SenderLabel" />
                        <Label Grid.Row="1" Content="{Binding Subject}" FontSize="12" HorizontalAlignment="Left" />
                        <Label Grid.Column="1" Content="{Binding Date}" FontSize="12" HorizontalAlignment="Right" />
                    </Grid>
                    <DataTemplate.Triggers >
                        <DataTrigger Binding="{Binding RelativeSource=
                                                {RelativeSource Mode=FindAncestor, AncestorType=
                                                {x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                            <Setter TargetName="SenderLabel" Property="FontWeight" Value="Light"/>
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

3 个答案:

答案 0 :(得分:0)

在您的视图模型中执行此操作,即为列表项提供属性(例如&#34;突出显示&#34;)然后在SelectedItem setter中,在选择项时将其设置为true。然后在您的XAML中设置列表项的样式,以便在设置“突出显示”时将文本设置为粗体。

XAML非常强大,可以做很多聪明的事情,但你用它做的事情很容易进行单元测试或调试。在视图模型中显式实现这样的功能,并仅将XAML限制为松散绑定的UI元素。

答案 1 :(得分:0)

我会在项目中拥有像#34; WasSelected&#34;并使用转换器将FontWeight绑定到该属性。

答案 2 :(得分:0)

您可以通过在Email ViewModel中添加“IsRead”属性来实现此目的。然后,只要发生新选择,就可以将IsRead属性设置为true。 “IsRead”属性将在发件人标签的触发器内使用,以更改其字体粗细。见下文。

XAML:

  <ListBox Grid.Row="1" ItemsSource="{Binding EmailsCollection}" Margin="5"
             SelectedItem="{Binding SelectedItem}"
             IsSynchronizedWithCurrentItem="True">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Label Content="{Binding Sender}" x:Name="SenderLabel">
                        <Label.Style>
                            <Style TargetType="Label">
                                <Setter Property="FontWeight" Value="Bold"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsRead}" Value="true">
                                        <Setter Property="FontWeight" Value="Light"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Label.Style> 
                    </Label>
                    <Label Grid.Row="1" Content="{Binding Subject}" FontSize="12" HorizontalAlignment="Left" />
                    <Label Grid.Column="1" Content="{Binding Date}" FontSize="12" HorizontalAlignment="Right" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

MainViewModel:

public class MainViewModel : ViewModelBase
{
    public ObservableCollection<Email> EmailsCollection { get; private set; }

    private Email _selectedItem;
    public Email SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            if (_selectedItem.IsRead) return;
            if (value != null)
            {
                EmailsCollection[EmailsCollection.IndexOf(value)].IsRead = true;
            }
        }
    }

    public MainViewModel() 
    {
        EmailsCollection = new ObservableCollection<Email>()
        {      
            new Email
            {
                Sender = "Sender",
                Subject = "Subject",
                Date = "Date"
            },
            new Email
            {
                Sender = "Sender1",
                Subject = "Subject1",
                Date = "Date1"
            },
            new Email
            {
                Sender = "Sender2",
                Subject = "Subject2",
                Date = "Date2"
            }
        };
    }
}

电子邮件ViewModel:

   public class Email : ViewModelBase
    {
        private bool _isRead;
        public bool IsRead
        {
            get { return _isRead; }
            set
            {
                _isRead = value;
                OnPropertyChanged("IsRead");
            }
        }

        public string Sender { get; set; }
        public string Subject { get; set; }
        public string Date { get; set; }
    }