• Webseitenerstellung und -hosting
  • .NET Anwendungsentwicklung
  • softwaregesteuerte Prozessoptimierung
  • .NET

    Programmierung

  • WebseitenErstellung

    undHosting

  • softwaregesteuerte

    Prozessoptimierung

  • Konzeption

    und Beratung

%PM, %15. %669 %2013 %15:%Apr

WPF Datagrid Spalten ein- und ausblenden

geschrieben von
Artikel bewerten
(0 Stimmen)
Leider besteht im WPF Datagrid keine Möglichkeit direkt auf Spalten zuzugreifen. Man kann natürlich durch die Eigenschaft "AutoGenerateColumns=True" und dem Ereignis "AutoGeneratingColumn" des Grid im Code Spalten anzeigen oder auch nicht. Was aber wenn man das ganze dynamisch gestalten will, am besten noch mit direkter Datenbindung und/oder seine Columns im Xaml definieren will? -> Fehlanzeige, da der Datacontext des Grids z.B. nicht an die DataGridTemplateColumn durchgereicht wird. Warum weiss der Geier. Abhilfe schafft folgendes Szenario von Thomas Levesque Leicht abgeändert und nach VB konvertiert. Zuerst braucht man einen Converter der boolsche Werte nach Visibility convertiert:

Public Class VisibilityConverter
Implements IValueConverter

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
Try
If value Is Nothing OrElse CBool(value) = False Then Return Visibility.Collapsed

Return Visibility.Visible

Catch ex As Exception
Return Visibility.Collapsed
End Try

end class

End Function
dann eine Klasse die Freezable erbt und eine DependencyProperty "Data" bereitstellt.

Public Class BindingProxy
Inherits Freezable
'Freezable überschreiben
Protected Overrides Function CreateInstanceCore() As Freezable
Return New BindingProxy()
End Function

'Diesen Namen (Data) in der dataProperty und im XAML verwenden
Public Property Data() As Object
Get
Return DirectCast(GetValue(DataProperty), Object)
End Get
Set(value As Object)
SetValue(DataProperty, value)
End Set
End Property

'DependencyProperty für die "Data"
Public Shared ReadOnly DataProperty As DependencyProperty = DependencyProperty.Register("Data", GetType(Object), GetType(BindingProxy), New UIPropertyMetadata(Nothing))
End Class


In den PageResources bzw. Datagridresources diese beiden Klassen referenzieren:











dann die proxy klasse des Grids bestücken. Ich mach das via code


Dim p As BindingProxy = CType(grdElementeBearbeitung.Resources("proxy"), BindingProxy)
p.Data = CType(grdBereiche.SelectedItem, text_bereich)


und die Visiblity der Spalte an die Data Property bzw. das Feld binden


Visibility="{Binding Data.spalte_anzeigen,Converter={StaticResource VisibilityConverter},Source={StaticResource proxy}}"

		
Gelesen 54763 mal Letzte Änderung am %PM, %24. %643 %2015 %14:%Jun

150 Kommentare

  • Kommentar-Link gabapentin 600 mg %PM, %26. %696 %2021 %15:%Nov gepostet von gabapentin 600 mg

    neurontin seizure gabapentin 100mg price benadryl and neurontin taken together how to increase gabapentin bioavailability

  • Kommentar-Link furosemide for sale %AM, %26. %451 %2021 %09:%Nov gepostet von furosemide for sale

    phenibut and modafinil purchase provigil what kind of drug is provigil what is provigil and how does it work

  • Kommentar-Link buy provigil near me %PM, %25. %616 %2021 %13:%Nov gepostet von buy provigil near me

    prednisone with ibuprofen purchase prednisone for dogs online without a prescription best time of day to take prednisone what are some of the side effects of prednisone?

  • Kommentar-Link gabapentin 1000mg %PM, %25. %586 %2021 %13:%Nov gepostet von gabapentin 1000mg

    prednisone and warfarin buy prednisone online without prescription prednisone 40 mg side effects what are the benefits of taking prednisone?

  • Kommentar-Link furosemide 40mg tabs %AM, %24. %115 %2021 %01:%Nov gepostet von furosemide 40mg tabs

    ceftriaxone and azithromycin zithromax capsules australia how much azithromycin to treat chlamydia what is azithromycin 500mg used to treat

  • Kommentar-Link stromectol generic %PM, %22. %992 %2021 %22:%Nov gepostet von stromectol generic

    prednisone and drinking buy prednisone from canada prednisone dosage for back pain how long till prednisone works

  • Kommentar-Link modafinil prices %PM, %21. %952 %2021 %21:%Nov gepostet von modafinil prices

    lasix im what is the generic name for lasix how much does furosemide cost

  • Kommentar-Link buy amoxicillin 500 %PM, %21. %902 %2021 %20:%Nov gepostet von buy amoxicillin 500

    penicillin and amoxicillin order amoxil 250mg amoxicillin and weed amoxicillin dosage pediatric

  • Kommentar-Link ivermectin 9mg %PM, %20. %824 %2021 %18:%Nov gepostet von ivermectin 9mg

    proair vs ventolin ventolin over the counter usa how long is albuterol good for what drug class is albuterol

  • Kommentar-Link amoxicilin brand name %PM, %20. %773 %2021 %17:%Nov gepostet von amoxicilin brand name

    amoxil vs moxtag antibiotics for sale amoxicillin amoxil 250mg tid with dialysis amoxicillin for syphilis

Schreibe einen Kommentar

Bitte achten Sie darauf, alle Felder mit Stern * zu füllen. HTML-Code ist nicht erlaubt.