window.resources中的BoolToVisibilityConverter

时间:2015-01-20 12:27:51

标签: c# wpf mvvm converter

我有一个列表框,其中包含用户可以更改的标签和文本框。列表框内容在数据模板中定义(在window.resources内)。我想为使用booltovisibility转换器更改的列表中的每个项目添加边框。

我想我遇到了麻烦,因为我正试图在window.resources中设置转换器。

有人可以指出我正确的方向吗?

查看型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace MaintainPersonData
{
    public class MaintainPersonViewModel
    {
        public MaintainPersonViewModel(ObservableCollection<PersonViewModel> personList)
        {
        }

        public INotifyUser Notifier;

        private ObservableCollection<PersonViewModel> _personList;
        public ObservableCollection<PersonViewModel> PersonList
        {
            get
            {
                return _personList;
            }
            set
            {
                _personList = value;
                OnPropertyChanged("PersonList");
            }
        }

        private bool _changesMade;
        public bool ChangesMade 
        {
            get 
            {
                return _changesMade; 
            }
            set
            {
                _changesMade = value;
                OnPropertyChanged("ChangesMade");
            }
        }

        private bool _hasErrors;
        public bool HasErrors
        {
            get { return _hasErrors; }
            set
            {
                _hasErrors = value;
                if (!_hasErrors)
                {
                    ErrorMessage = "";
                }
                OnPropertyChanged("HasErrors");
            }
        }

的Xaml:

<Window.Resources>
    <conv:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>

    <DataTemplate x:Key="ListBoxItemTemplate">
        <Border BorderBrush="LightGreen" BorderThickness="2" Visibility="{Binding ChangesMade, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource BoolToVisibilityConverter}}">
        <Grid>
            <TextBox x:Name="PersonTextBox" Text="{Binding PersonName, UpdateSourceTrigger=PropertyChanged}"/>
        </Grid>
    </Border>
   </DataTemplate>
</Window.Resources>

<Grid>
   <ListBox x:Name="PersonListBox" SelectionMode="Single" KeyboardNavigation.TabNavigation="Continue" ItemTemplate="{StaticResource ListBoxItemTemplate}" ItemsSource="{Binding PersonList}">
        <!-- Code to highlight selected item (http://stackoverflow.com/questions/15366806/wpf-setting-isselected-for-listbox-when-textbox-has-focus-without-losing-selec) -->
    </ListBox>

    <!-- BoolToVisibilityConverter works perfectly here -->
    <Label Name="ErrorLabel" Grid.Column="0" Foreground="Red" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="{Binding HasErrors, Converter={StaticResource BoolToVisibilityConverter}}" >
            <TextBlock Text="{Binding ErrorMessage, UpdateSourceTrigger=PropertyChanged}" />
        </Label>
     </Grid>       
</Window>

最后,转换器:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace MaintainRegexData
{
    class BoolToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

首先,您将ItemsSource绑定到PersonList,其类型为ObservableCollection<PersonViewModel>,其中ChangesMade包含在MaintainPersonViewModel中,因此您需要放置ChangesMade PersonViewModel 1}}在personName类内,并在{{1}}属性更改时进行更改。

并且不要忘记@Frank对边境的评价。

答案 1 :(得分:1)

每个ChangesMade应该定义

PersonViewMode,因为现在您将(或不会)为所有项目添加边框。您是否检查过调用BoolToVisibilityConverter的转换方法?最后一点 - 在窗口资源中设置转换器没有任何问题。