Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Grüße,

ich bin grad dabei, ein kleines Optionsfenster für Individualisierungen zu schreiben. Unter Anderem sollen da bestimmte Farben zugewiesen werden können. Irgendwie klappt das aber noch nicht so ganz, wie ich will.

Zunächst hab ich erstmal mittels Helperclass die Enum der Farben erzeugt:

        <ObjectDataProvider x:Key="PropertyCollector" MethodName="GetPropNames" ObjectType="{x:Type local:PropertyCollector}">

            <ObjectDataProvider.MethodParameters>

                <x:Type TypeName="Colors"/>

            </ObjectDataProvider.MethodParameters>

        </ObjectDataProvider>
Hilfsmethode ist simpel:
        public IEnumerable GetPropNames(Type type)

        {

            foreach (PropertyInfo pi in type.GetProperties())

                yield return pi.GetValue(null, null);

        }
Passt auch soweit. Das ganze ist der ItemsSource für meine ComboBox:
<ComboBox Grid.Row="0" Grid.Column="1" Margin="5"

                  ItemsSource="{Binding Mode=OneWay, Source={StaticResource PropertyCollector}}" 

                  ItemTemplate="{StaticResource ColorTemplate}" 

                  SelectedItem="{Binding BackgroundColorOption.Value}"/>
Damit man auch was erkennt und nicht nur "#00000000"-Strings dastehen, soll als Repräsentation ein kleines Viereck, mit Farbe gefüllt, gezeichnet werden:
        <DataTemplate x:Key="ColorTemplate">

            <StackPanel Orientation="Horizontal">

                <Rectangle Width="40" Stroke="#FF000000">

                    <Rectangle.Fill>

                        [COLOR="Red"]<SolidColorBrush Color="{Binding}"/>[/COLOR]

                    </Rectangle.Fill>

                </Rectangle>

                <TextBlock Text =" " Foreground="Black" />

            </StackPanel>

        </DataTemplate>

(Anm. d. R.: Leerer TextBlock für Ein-Zeilen-Höhe)

Irgendwie mag WPF das aber nicht, im Output steht immer:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:(no path); DataItem=null; target element is 'SolidColorBrush' (HashCode=273093); target property is 'Color' (type 'Color')

Programm stürzt nicht ab und es kommt der Fehler nur, wenn ich das Optionsfenster öffne. Frag mich auch, wie DataItem null sein soll.

Jemand ne Idee?

  • Autor

Ich nutze Ressourcen, das Template und die Liste sind als StaticResources im Usercontrol hinterlegt. (Will ja nicht 1000. Mal die Liste neu erstellen)

<ComboBox Grid.Row="0" Grid.Column="1" Margin="5"

                  ItemsSource="{Binding Mode=OneWay, Source={[COLOR="Red"]StaticResource[/COLOR] PropertyCollector}}" 

                  ItemTemplate="{[COLOR="Red"]StaticResource[/COLOR] ColorTemplate}" 

                  SelectedItem="{Binding BackgroundColorOption.Value}"/>

Dies entspricht, zumindest nach meiner Ansicht, FindResource() (nur halt XAML-Code). Zumindest mach ich das häufig so, aber der Fehler trat bisher komischerweise noch nie auf.

Ich nutze Ressourcen, das Template und die Liste sind als StaticResources im Usercontrol hinterlegt. (Will ja nicht 1000. Mal die Liste neu erstellen)

Ich meinte mit Resourcen ein ResourceDictionary (reine XAML-Datei), in der Dein jeweiliger Brush abgelegt wird und wenn ein Benutzer eine Auswahl trifft, wird eben diese Resource geladen.

Meine Überlegung: Warum sollte ich etwas schreiben, was es schon gibt?

Die Methode FindResource(), die das ResourceDictionary lädt, funktioniert (leider) nur Code-Behind.

  • Autor
Ich meinte mit Resourcen ein ResourceDictionary (reine XAML-Datei), in der Dein jeweiliger Brush abgelegt wird und wenn ein Benutzer eine Auswahl trifft, wird eben diese Resource geladen.

Weil die Objekte vom Typ Color sind und nicht vom Typ Brush. Und da ist es ja sinnvoller, wenn ich eh schon eine Aufzählung von Colors habe, den Brush dynamisch als Template zu erstellen (ist ja eh immer ein SCB). Bei einem ResourceDictionary müsste immer erst nachgesehen werden "Welche Farbe ist es? Aha. Nehme den Brush." (oder so)

Mir kam aber grad die Erleuchtung: Wenn WPF zu "dumm" ist, den Brush richtig zu erstellen, mach ichs halt selber.


        <local:ColorBrushConverter x:Key="BrushConverter"/>

        <DataTemplate x:Key="ColorTemplate">

            <StackPanel Orientation="Horizontal">

                <Rectangle Width="40" Fill="{Binding Converter={StaticResource BrushConverter}}" Stroke="#FF000000"/>

                <TextBlock Text =" " Foreground="Black" />

            </StackPanel>

        </DataTemplate>

    internal class ColorBrushConverter : IValueConverter

    {

        #region Constructors


        public ColorBrushConverter()

        {

        }


        #endregion


        #region Methods


        #region IValueConverter Members


        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            if (value is Color)

                return new SolidColorBrush((Color)value);

            else if (value is SolidColorBrush)

                return ((SolidColorBrush)value).Color;

            return null;

        }


        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            if (value is Color)

                new SolidColorBrush((Color)value);

            else if (value is SolidColorBrush)

                return ((SolidColorBrush)value).Color;

            return null;

        }


        #endregion


        #endregion

    }

Geht. :rolleyes:

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.