WPF Bind Background для ListView ItemContainerStyle - PullRequest
0 голосов
/ 30 мая 2018

У меня есть UserControl (FahrtControl.xaml) с ListView.Это UserControl связано с ItemsControl в моем MainWindow.xaml.MainWindow имеет свою собственную ViewModel, а FahrtControl имеет свою собственную ViewModel.Теперь я хочу привязать фон элементов Listview к свойству Brush в ViewModel из FahrtControl.

Вот соответствующие части моего кода:

MainWindow.xaml:

<Window x:Class="WpfFrontend.Forms.Main.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfFrontend.Forms.Main"
    xmlns:fahrtControl="clr-namespace:WpfFrontend.Controls.FahrtControl"
    mc:Ignorable="d">
<Window.DataContext>
    <local:MainViewModel />
</Window.DataContext>
<Window.Resources>
    <DataTemplate DataType="{x:Type fahrtControl:FahrtControlViewModel}">
        <fahrtControl:FahrtControl />
    </DataTemplate>
</Window.Resources>
<ItemsControl ItemsSource="{Binding Fahrten, UpdateSourceTrigger=PropertyChanged}" />

MainViewModel.cs:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
using System.Windows.Media;

using Backend;

using DatabaseCommunication;

using WpfFrontend.Annotations;
using WpfFrontend.Controls.FahrtControl;


namespace WpfFrontend.Forms.Main
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel ()
        {
            SyncFahrten ();
        }

        private void SyncFahrten ()
        {
            var fahrtenPromise =
                MainUtility.GetFahrtenToRangeAsync (GlobalProperties.Start, GlobalProperties.Start.AddDays (6));

            fahrtenPromise.Task.GetAwaiter ().OnCompleted (() =>
            {
                AddFahrten (fahrtenPromise.Task.Result);
            });
        }

        private void AddFahrten (List <ExtendedFahrt> fahrten)
        {
                foreach (var fahrtControlViewModel in
                    fahrten.Select (fahrt =>
                                 {
                                     return new FahrtControlViewModel (
                                         Brushes.Red, Brushes.Red, Brushes.White,
                                         fahrt.Ort,
                                         new ObservableCollection <string>
                                         {
                                             fahrt.Bemerkung
                                         }, new ObservableCollection <KundeDisplay> (fahrt.Kunden));
                                 }))
                    Fahrten.Add (fahrtControlViewModel);

            OnPropertyChanged ("");
        }



        private ObservableCollection <FahrtControlViewModel> _fahrten =
            new ObservableCollection <FahrtControlViewModel> ();

        public ObservableCollection <FahrtControlViewModel> Fahrten
        {
            get => _fahrten;
            set
            {
                if (Equals (value, _fahrten))
                    return;
                _fahrten = value;
                OnPropertyChanged ();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged ([CallerMemberName] string propertyName = null) =>
            PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
    }
}

FahrtControl.xaml:

<UserControl x:Class="WpfFrontend.Controls.FahrtControl.FahrtControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:WpfFrontend.Controls.FahrtControl"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Style x:Key="HeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="Visibility" Value="Collapsed" />
        </Style>
    </UserControl.Resources>
    <ListView ItemsSource="{Binding Kunden}"
              Background="{Binding KundenBrush, UpdateSourceTrigger=PropertyChanged}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Background" Value="{Binding DataContext.KundenBrush}" />
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView ColumnHeaderContainerStyle="{StaticResource HeaderStyle}">
                <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" />
            </GridView>
        </ListView.View>
    </ListView>
</UserControl>

FahrtControlViewModel.cs:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Media;

using Backend;

using WpfFrontend.Annotations;
using WpfFrontend.Misc;


namespace WpfFrontend.Controls.FahrtControl
{
    public class FahrtControlViewModel : INotifyPropertyChanged
    {
        private Brush                               _kundenBrush = Brushes.Red;
        private ObservableCollection <KundeDisplay> _kunden;

        /// <inheritdoc />
        public FahrtControlViewModel (
            Brush                               kundenBrush,
            ObservableCollection <KundeDisplay> kunden)
        {
            Kunden         = kunden;
            KundenBrush    = kundenBrush;
        }
        public Brush KundenBrush
        {
            get => _kundenBrush;
            set
            {
                if (value.Equals (_kundenBrush))
                    return;
                _kundenBrush = value;
                KundenDark   = _kundenBrush.IsDark ();

                OnPropertyChanged ();
            }
        }

        public ObservableCollection <KundeDisplay> Kunden
        {
            get => _kunden;
            set
            {
                if (Equals (value, _kunden))
                    return;
                _kunden = value;
                OnPropertyChanged ();
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged ([CallerMemberName] string propertyName = null) =>
            PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
    }
}

Я уже пробовал следующее:

И если бы мне пришлось угадывать каждое другое предложение, слегка связанное с темой.Что я здесь не так делаю?Это как-то связано с тем, что я использую UserControl внутри ItemsControl?Другие привязки свойств, не заключенные в тег стиля, работают внутри my UserControl, поэтому он должен иметь отношение к тегу стиля, не так ли?

1 Ответ

0 голосов
/ 30 мая 2018

DataContext из ListView.ItemContainerStyle - это не то же самое, что ListView.Вы можете найти правильный контекст данных с его именем элемента:

<UserControl x:Class="WpfFrontend.Controls.FahrtControl.FahrtControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:WpfFrontend.Controls.FahrtControl"
             mc:Ignorable="d"

             <!--      -->
             x:Name="root"
             <!--      -->

             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Style x:Key="HeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="Visibility" Value="Collapsed" />
        </Style>
    </UserControl.Resources>
    <ListView ItemsSource="{Binding Kunden}"
              Background="{Binding KundenBrush, UpdateSourceTrigger=PropertyChanged}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">

             <!--      -->
                <Setter Property="Background" Value="{Binding ElementName=root, Path=DataContext.KundenBrush}" />
             <!--      -->
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView ColumnHeaderContainerStyle="{StaticResource HeaderStyle}">
                <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" />
            </GridView>
        </ListView.View>
    </ListView>
</UserControl>

Или проследив до дерева элементов:

<UserControl x:Class="WpfFrontend.Controls.FahrtControl.FahrtControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:WpfFrontend.Controls.FahrtControl"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Style x:Key="HeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="Visibility" Value="Collapsed" />
        </Style>
    </UserControl.Resources>
    <ListView ItemsSource="{Binding Kunden}"
              Background="{Binding KundenBrush, UpdateSourceTrigger=PropertyChanged}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">

             <!--      -->
                <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource AncestorType=FahrtControl}, Path=DataContext.KundenBrush}" />
             <!--      -->
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView ColumnHeaderContainerStyle="{StaticResource HeaderStyle}">
                <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" />
            </GridView>
        </ListView.View>
    </ListView>
</UserControl>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...