سؤال

تحيات،

أنا أستخدم WPF مع نمط عرض موديل-عرض ، ولدي نموذج عرض مع IsSelected الخاصية التي أريد ربطها بـ TreeViewItemIsSelected خاصية للجميع TreeViewItemS في النطاق. أنا أحاول القيام بذلك مع Style و Setter. هذا يعمل على ما يبدو للمستوى الجذر TreeViewItemS ، ولكن ليس لأطفالهم. لماذا هذا؟ كيف يمكنني تطبيق هذا على الجميع TreeViewItem ضوابط؟

هنا هو عرض XAML:

<UserControl x:Class="MyApp.AllAreasView"
             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:MyApp="clr-namespace:MyApp"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="700">
<UserControl.Resources>
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsSelected"
                Value="{Binding IsSelected, Mode=TwoWay}"/>
    </Style>

    <MyApp:CountVisibilityConverter x:Key="CountVisibilityConverter" />

    <HierarchicalDataTemplate x:Key="AreaTemplate"
                              DataType="AreaViewModel"
                              ItemsSource="{Binding Path=SubareasCollectionView}">
        <WrapPanel>
            <TextBlock Text="{Binding Path=Name}" Margin="0 0 8 0" />
            <TextBlock DataContext="{Binding Path=Subareas}" 
                       Text="{Binding Path=Count, StringFormat= (\{0\})}"
                       Visibility="{Binding Path=Count, Converter={StaticResource CountVisibilityConverter}}" />
        </WrapPanel>
    </HierarchicalDataTemplate>
</UserControl.Resources>

<TreeView ItemsSource="{Binding TopLevelAreas}"
          ItemTemplate="{StaticResource AreaTemplate}">
</TreeView>

</UserControl>
هل كانت مفيدة؟

المحلول

أعتقد أننا سنحتاج إلى مزيد من المعلومات للإجابة على سؤالك. على وجه التحديد ، كيف يبدو نموذج (موالد) عرضك. فيما يلي مثال يمكنك نسخه ولصقه بشكل جيد.

window1.xaml:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="Background" Value="{Binding Background}"/>
        </Style>

        <HierarchicalDataTemplate x:Key="ItemTemplate" DataType="local:DataItem" ItemsSource="{Binding Path=Children}">
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
    </Window.Resources>

    <TreeView ItemsSource="{Binding}" ItemTemplate="{StaticResource ItemTemplate}"/>
</Window>

window1.xaml.cs:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            var dis = new ObservableCollection<DataItem>();
            var di = new DataItem() { Name = "Top", Background = Brushes.Red };
            di.Children.Add(new DataItem() { Name = "Second", Background = Brushes.Blue });

            dis.Add(di);
            DataContext = dis;
        }
    }

    public class DataItem
    {
        public DataItem()
        {
            Children = new ObservableCollection<DataItem>();
        }

        public string Name
        {
            get;
            set;
        }

        public ICollection<DataItem> Children
        {
            get;
            set;
        }

        public Brush Background
        {
            get;
            set;
        }
    }
}

نصائح أخرى

من خلال العمل مع نماذج العرض ، ستحصل على ودية للغاية مع خاصية ItemContainersTyle. ما كنت تفعله في الكود الخاص بك قم بتعيين قالب البيانات لمستوى الجذر. ما تريد القيام به هو نمط كل عنصر من العناصر الموجودة في TreeView ، والتي يمكنك القيام بها مثل ذلك.

<TreeView ItemsSource="{Binding TopLevelAreas}"
          ItemContainerStyle="{StaticResource AreaTemplate}">
</TreeView>

التمتع

عليك أن تستخدم كما هو مذكور أدناه. استفد من خيار المستند

<TreeView ItemsSource="{Binding TopLevelAreas}">
   <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource StyleKey}"/>
   </TreeView.Resources>
</TreeView>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top