Dependency properties

0
public int FontSize
    {
        get { return (int)FSProperty; }
        set { FSProperty = value; }
    }

    public static readonly DependencyProperty FSProperty =
        DependencyProperty.Register("FontSize", typeof(int), typeof(MainWindow), new PropertyMetadata(30));

próbuję się nauczyć dependency properties z WPF.
dostaję errory:

  1. przestrzeń nazw nie może bezpośrednio zawierać skłądowych takich jak pola lu metody
  2. nazwa FSProperty nie istnieje w bieżącym kontekście

jak zrobić żeby ustawiać za pomocą DP rozmiar czcionki w przycisku?

0

Masz napisane w 1. błędzie o co chodzi, wrzuć to property do jakiejś klasy.

0
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        
        public int MyFontSize
        {
            get { return  (int)GetValue(FSProperty); }
            set { SetValue(FSProperty, value); }
        }

        public static readonly DependencyProperty FSProperty =
            DependencyProperty.Register("MyFontSize", typeof(int), typeof(MainWindow), new PropertyMetadata(30));

        public Color MyColor
        {
            get { return (Color)GetValue(ColorProperty); }
            set { SetValue(ColorProperty,value); }
        }

        public static readonly DependencyProperty ColorProperty =
            DependencyProperty.Register("MyColor", typeof(Color), typeof(MainWindow), new PropertyMetadata(Colors.Aqua));


        private void click(object sender, RoutedEventArgs e)
        {
            SetValue(FSProperty, 50);
            SetValue(ColorProperty, Colors.Red);
           
        }
    }
x:Name="MyWindow"
...
<Button 
                Click="click" Name="b1" Content="SOME CONTENT" 
                FontSize   ="{Binding ElementName=MyWindow, Path=MyFontSize }" 
                Background ="{Binding ElementName=MyWindow, Path=MyColor }"
                Width="500" Height="100"/>

mam coś takiego, zmiana rozmiaru czcionki działa, ale button jest bez koloru, co robię źle?

0

Background jest typu Brush, a nie Color.

1 użytkowników online, w tym zalogowanych: 0, gości: 1