0

全て -

Observable コレクション (VM にもある) の現在のアイテムに基づいて、VM にパブリック プロパティを設定しようとしています。基本的に、行に基づいて、shadecolor を Blue または Pink に設定したいと考えています (以下のサンプル コードを参照)。最終結果がどのように見えるかの画像も参照してください。

誰かが提案してもらえますか-どうすればこれを達成できますか-この問題に本当に悩まされています

以下のサンプルコードを参照してください。

Model.cs

public class Model
{
    public Employee empdetails { get; set; }
}

public class Employee
{
    public string fname { get; set; }
    public string lname { get; set; }
    public Enum gender { get; set; }
}

public enum gender
{
    Male,
    Female
}

ViewModel.cs

public class ViewModel
{
    public ObservableCollection<Model> employees {get; set;}
    public myCommand NextCommand { get; set; }
    private Color _shadecolor;

    public Color shadecolor
    {
        get
        {
            return _shadecolor;
        }
        set
        {
            _shadecolor = value;
        }
    }

    public ViewModel()
    {
        employees = new ObservableCollection<Model>()
        {
            #region Populating Emp 1
            new Model()
            {
                empdetails = new Employee()
                {
                    fname = "John",
                    lname = "Smith",
                    gender = gender.Male
                }
            },
            #endregion

            #region Populating Emp 2
            new Model()
            {
                empdetails = new Employee()
                {
                    fname = "Robert",
                    lname = "Ally",
                    gender = gender.Female
                }
            },
            #endregion
        };

        NextCommand = new myCommand(myNextCommandExecute, myCanNextCommandExecute);
    }

    private void myNextCommandExecute(object parameter)
    {

    }

    private bool myCanNextCommandExecute(object parameter)
    {
        return true;
    }
}

View.xaml

<Window x:Class="WpfApplication1.View"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="View" Height="500" Width="500"  WindowStyle="None" AllowsTransparency="True" Background="Transparent">
<Border VerticalAlignment="Top" HorizontalAlignment="Left" BorderBrush="Silver" BorderThickness="2" CornerRadius="15">
    <Border.Background>
        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.511,0.957">
            <GradientStop Color="LightGray" Offset="0.55" />
            <GradientStop Color="{Binding shadecolor}" Offset="1.3" />
        </LinearGradientBrush>
    </Border.Background>
    <Grid Width="300" Height="300" Margin="3">
        <StackPanel VerticalAlignment="Top" >
            <TextBlock Text="{Binding Path=employees/empdetails.fname}" />
            <Button Command="{Binding NextCommand}" Content="Next" Width="100"></Button>
        </StackPanel>
    </Grid>
</Border>
</Window>

ここに画像の説明を入力

4

3 に答える 3

1

あなたが望むのは、 SelectedItem={Binding SelectedItem} をバインドすることだと思います。ここで、選択されたアイテムはビューモデルにもあり、監視可能なプロパティとして公開されます。

public Model SelectedItem
{
   ...
}

Selector から派生した XAML に何もないため、ここで何を達成しようとしているのか完全にはわかりません。したがって、ここには選択された項目の概念はありません。

于 2012-10-08T20:49:55.230 に答える
1

ValueConverter を使用しないのはなぜですか?

<GradientStop Color="{Binding Path=gender, Converter={StaticResource GenderToColorConverter}" Offset="1.3" />

次に、値コンバーター内で次のようにします。

If value == Gender.Male return blue; 
return pink;

技術的には、ブラシを返すと思いますが、それについては引用しないでください。

サンプルコードは次のとおりです。

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = new ViewModel(GetTestEmployees());
        }

        static IEnumerable<Employee> GetTestEmployees()
        {
            return new[]
            {
                new Employee()
                {
                    FirstName = "Tom",
                    LastName = "Selleck",
                    Gender = Gender.Male
                },
                new Employee()
                {
                    FirstName = "Pat",
                    LastName = "Sajak",
                    Gender = Gender.Male,
                },
                new Employee()
                {
                    FirstName = "Mae",
                    LastName = "West",
                    Gender = Gender.Female
                }
            };
        }
    }

    public class ViewModel : INotifyPropertyChanged
    {
        public ViewModel(IEnumerable<Employee> employees)
        {
            _employees = new ObservableCollection<Employee>(employees);
            SelectedEmployee = employees.First();
        }

        ObservableCollection<Employee> _employees;
        public ObservableCollection<Employee> Employees
        {
            get { return _employees; }
        }

        Employee _selectedEmployee;
        public Employee SelectedEmployee 
        {
            get { return _selectedEmployee; }
            set
            {
                _selectedEmployee = value;
                RaisePropertyChangedEvent("SelectedEmployee");
            }
        }

        public void Next()
        {
            var curr = Employees.IndexOf(_selectedEmployee);

            if (curr == -1) throw new ArgumentOutOfRangeException();

            var next  = (curr + 1) % Employees.Count;

            SelectedEmployee = Employees[next];
        }

        ICommand _nextCommand;
        public ICommand NextCommand
        {
            get
            {
                if (_nextCommand == null)
                    _nextCommand = new NextCommand(this);

                return _nextCommand;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChangedEvent(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class NextCommand : ICommand
    {
        ViewModel _viewModel;

        public NextCommand(ViewModel viewModel)
        {
            _viewModel = viewModel;
        }

        public bool CanExecute(object parameter)
        {
            //throw new NotImplementedException();

            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            //throw new NotImplementedException();

            _viewModel.Next();
        }
    }

    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Gender Gender { get; set; }
    }

    public enum Gender
    {
        Male,
        Female
    }

    public class GenderToColorConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var gender = (Gender)value;

            if (gender == Gender.Male)
            {
                return Colors.Blue;
            }

            return Colors.Pink;
        }

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


}
}

対応するマークアップは次のとおりです。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:loc="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <loc:GenderToColorConverter x:Key="GenderToColorConverter"/>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding Employees}"
                      SelectedItem="{Binding SelectedEmployee}">
            <ListBox.Template>
                <ControlTemplate TargetType="ListBox">
                    <Grid>
                        <ContentControl DataContext="{TemplateBinding SelectedItem}">
                            <StackPanel >
                                <StackPanel.Background>
                                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.511,0.957">
                                        <GradientStop Color="LightGray" Offset="0.55" />
                                        <GradientStop Color="{Binding Path=Gender, Converter={StaticResource GenderToColorConverter}}" Offset="1.3" />
                                    </LinearGradientBrush>
                                </StackPanel.Background>
                                <TextBox Text="{Binding FirstName}"/>
                                <TextBox Text="{Binding LastName}"/>
                            </StackPanel>
                        </ContentControl>
                    </Grid>
                </ControlTemplate>
            </ListBox.Template>

        </ListBox>

        <Button VerticalAlignment="Bottom" HorizontalAlignment="Center" Content="Next" Command="{Binding NextCommand}"/>
    </Grid>
</Window>
于 2012-10-08T21:12:55.093 に答える