سؤال

لقد حصلت على أ TreeView, ، حيث أستخدم HierarchicalDataTemplate. وبعد مع هذا، أنا ألون عناصري المختلفة (كل نفس النوع).

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

إليك عينة من التعليمات البرمجية الخاصة بي:

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

        TreeElements tRoot = new TreeElements("Root", false);
        TreeElements t1 = new TreeElements("Node 1", false);
        TreeElements t11 = new TreeElements("Node 1-1", true);
        t1.Children.Add(t11);
        TreeElements t12 = new TreeElements("Node 1-2", false);
        t1.Children.Add(t12);
        tRoot.Children.Add(t1);
        TreeElements t2 = new TreeElements("Node 2", false);
        TreeElements t21= new TreeElements("Node 2-1", false);
        TreeElements t211 = new TreeElements("Node 2-1-1", false);
        t21.Children.Add(t211);
        t2.Children.Add(t21);
        tRoot.Children.Add(t2);
        trv.Items.Add(tRoot);
    }
}

public class TreeElements
{
    public string Description { get; set; }
    public List<TreeElements> Children { get; set; }

    public TreeElements(string description, bool error)
    {
        Description = description;
        _error = error;
        Children = new List<TreeElements>();
    }

    private bool _error;

    public bool Error
    {
        get
        {
            bool bValue = _error;
            foreach (TreeElements child in Children)
                bValue = bValue || child.Error;
            return bValue;
        }
    }
}

و xaml:

<Window x:Class="WpfApplication2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <HierarchicalDataTemplate x:Key="HDT_items" ItemsSource="{Binding Path=Children}">
            <TextBlock Text="{Binding Path=Description}" x:Name="txt" />
            <HierarchicalDataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=Error}" Value="true">
                    <Setter TargetName="txt" Property="Foreground" Value="Red" />
                </DataTrigger>
            </HierarchicalDataTemplate.Triggers>
        </HierarchicalDataTemplate>    
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <CheckBox x:Name="chk">Mask some items</CheckBox>

        <TreeView Grid.Row="1" x:Name="trv" ItemTemplate="{StaticResource HDT_items}" />
    </Grid>
</Window>

أريد، عندما يتم التحقق من مربع الاختيار، لعدم عرض العقد مع الخطأ = خطأ، ولكن دون تغيير مصدر البيانات. هل من الممكن وكيف ؟

هل كانت مفيدة؟

المحلول

تغيير نوع Threeelement.Clildren من القائمة إلى الملاحظ وبدلا من إخفاء العناصر في الرأي، ما عليك سوى إزالة TME من المجموعة الأساسية في ViewModel.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top