在WPF应用程序中仅更改一次属性
本文关键字:一次 属性 应用程序 WPF | 更新日期: 2023-09-27 18:10:01
我如何在ListBox
中选择unbold
文本,并在删除选择后保持这种方式?在我的代码中,删除选择后返回粗体。
我已经尝试使用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>
你可以通过在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"
}
};
}
}
电子邮件视图模型:
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; }
}
在你的视图模型中这样做,即给你的列表项一个属性(例如:"Highlight"),然后在SelectedItem setter中,当项目被选中时,将该值设置为true。然后在XAML中设置列表项的样式,当高亮设置时将文本设置为粗体。
XAML非常强大,可以做很多聪明的事情,但是你用它做的事情不容易进行单元测试或调试。在视图模型中显式地实现这样的功能,并将XAML限制为仅松散绑定的UI元素。我将在项目中添加一个类似"WasSelected"的属性,并使用转换器将该属性的FontWeight绑定到该属性。