如何绑定到列表框中的特定属性

斯特列尼科夫·列夫(Strelnikov Lev)

我需要在第一次选择时更改列表项中文本的粗体。

Xaml:

<DockPanel >
        <TextBox DockPanel.Dock="Top" Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        <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}"  Name="SenderLabel" FontWeight="{Binding IsRead,  Converter={StaticResource Converter}}"/>
                        <!--Style="{StaticResource Sender}"-->
                        <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>
    </DockPanel>

查看模型:

public Email SelectedItem
    {
        get
        {
            return _selectedItem;
        }

        set
        {
            _selectedItem = value;
            _selectedItem.IsRead = true;
            OnPropertyChanged(this,"SelectedItem");

        }
    }

模型:

public bool IsRead
    {
        get { return _isRead; }
        set
        {
            _isRead = value;
            OnPropertyChanged(this, "IsRead");
        }
    }

如何绑定到列表中所选项目的“ IsRead”属性?当前的方式从一开始就遍及所有电子邮件,此后没有任何变化。

山姆3D3v

DataTrigger无需转换器即可简单使用

 <DockPanel >
        <TextBox DockPanel.Dock="Top" Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        <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}"  Name="SenderLabel" >
                            <Label.Style>
                                <Style TargetType="Label">
                                    <Setter Property="FontWeight" Value="Normal"/>
                                    <Style.Triggers>                                            
                                        <DataTrigger Binding="{Binding IsRead}" Value="true">
                                            <Setter Property="FontWeight" Value="Bold"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Label.Style>                                
                        </Label> 
                        <!--Style="{StaticResource Sender}"-->
                        <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>
    </DockPanel>

更新:此处对应的Model / ViewModel

  public class Email:INotifyPropertyChanged
{
    private bool _isRead;
    public bool IsRead
    {
        get { return _isRead; }
        set
        {
            _isRead = value;
            OnPropertyChanged();
        }
    }  
    private String _sender ;
    public String Sender
    {
        get
        {
            return _sender;
        }

        set
        {
            if (_sender == value)
            {
                return;
            }

            _sender = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

public partial class MainWindow : Window,INotifyPropertyChanged
{

    private ObservableCollection<Email> _emailsCollection = new ObservableCollection<Email>(){new Email(){Sender = "FirstSender",IsRead = true},new Email(){Sender = "SecondSender",IsRead = false}};

    public ObservableCollection<Email> EmailsCollection
    {
        get
        {
            return _emailsCollection;
        }

        set
        {
            if (_emailsCollection == value)
            {
                return;
            }

            _emailsCollection = value;
            OnPropertyChanged();
        }
    }
    private Email _selectedItem=new Email(){IsRead = true};
    public Email SelectedItem
    {
        get
        {
            return _selectedItem;
        }

        set
        {
            _selectedItem = value;
            _selectedItem.IsRead = true;
            OnPropertyChanged();

        }
    }
    public MainWindow()
    {
        InitializeComponent();            
    }
    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

输出

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将命令绑定到列表框项目?

来自分类Dev

在列表框项中绑定到ViewModel

来自分类Dev

将图像绑定到WPF中的列表框

来自分类Dev

将列表框绑定到XAML中的SelectionChanged ComboBox

来自分类Dev

在列表框项中绑定到ViewModel

来自分类Dev

无法将列表框绑定到类中的ObservableCollection

来自分类Dev

如何将列表框中的选定项目绑定到查询(MS Access)?

来自分类Dev

如何在列表框中绑定颜色集合(MVVM)

来自分类Dev

如何将组合框数据源绑定到Windows窗体中的窗体/列表框?

来自分类Dev

WPF中的列表框数据绑定

来自分类Dev

在列表框中绑定项目控件

来自分类Dev

在列表框中绑定符号

来自分类Dev

可观察的子属性字符串到列表框的绑定

来自分类Dev

将标签值设置为绑定到列表框的给定对象的属性的标签值

来自分类Dev

如何将自定义对象列表作为源绑定到列表框?

来自分类Dev

绑定到列表的列表框未更新

来自分类Dev

wpf将列表框项目绑定到静态列表

来自分类Dev

如何将其他数据绑定到asp.net列表框控件?

来自分类Dev

将功能绑定到列表框C#WPF中的文本块

来自分类Dev

将可观察集合中的可观察集合绑定到列表框项

来自分类Dev

ViewModel 和 View 未绑定到列表框中的用户控件

来自分类Dev

WPF将多个UserControl绑定到列表框

来自分类Dev

将所有系统颜色绑定到列表框

来自分类Dev

将随机颜色绑定到列表框的项目

来自分类Dev

将列表框绑定到数据库表

来自分类Dev

Windows Phone列表框ItemTemplate绑定到listBoxItem

来自分类Dev

将随机颜色绑定到列表框的项目

来自分类Dev

在绑定到字典的列表框中分组

来自分类Dev

为什么列表框不绑定到IEnumerable更新?

Related 相关文章

热门标签

归档