Я не могу связать фон динамически, потому что он вызывает исключение «A« DynamicResourceExtension »не может быть задано в свойстве« Source »типа« Binding ».« DynamicResourceExtension »может быть установлен только в DependencyProperty объекта DependencyObject».Как я могу связать фоновый цвет в WPF динамически?
<Window x:Class="TestWpfApplication.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestWpfApplication"
mc:Ignorable="d"
Title="Window2" Height="300" Width="300">
<Window.Resources>
<SolidColorBrush Color="Blue" x:Key="customColorBrush"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Background="{Binding Source={DynamicResource customColorBrush}}" Margin="20"></Button>
<Button Background="{Binding Source={StaticResource customColorBrush}}" Margin="20" Grid.Row="1" Click="Button_Click"></Button>
</Grid>
Вы должны лучше использовать привязки для структуры MVVM – Ugur