我在UWP應用程序中有一個網格在MainPage和另一個網格BlankPage1中,我想同時更改MainPage中網格的顏色和數據綁定的BlankPage1中的網格。使用數據綁定xaml更改其他頁面中網格的顏色UWP
代碼。
顏色類:
class ColorGridClass : INotifyPropertyChanged
{
private SolidColorBrush _coloreGenerale = new SolidColorBrush(Color.FromArgb(255, 16, 111, 151));
public SolidColorBrush ColoreGenerale
{
get => _coloreGenerale;
set
{
_coloreGenerale = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ColoreGenerale)));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
XAML的MainPage:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.DataContext>
<local:ColorGridClass x:Name="ColorOfGrid" ColoreGenerale="Aquamarine"/>
</Grid.DataContext>
<Button x:Name="btnChangeColor" Content="Change Color" Click="btnChangeColor_Click" HorizontalAlignment="Left" Margin="10,10,0,0" Foreground="{Binding }" VerticalAlignment="Top"/>
<Grid Background="{Binding ColoreGenerale, Mode=OneWay}" HorizontalAlignment="Left" Height="500" Margin="10,52,0,0" VerticalAlignment="Top" Width="500">
<TextBlock Text="Grid One" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,30,0,0"/>
</Grid>
<Frame x:Name="MainFrame" Content="" HorizontalAlignment="Left" Margin="532,10,0,0" VerticalAlignment="Top" Height="1060" Width="1378"/>
</Grid>
的MainPage xaml.cs:
public MainPage()
{
this.InitializeComponent();
MainFrame.Navigate(typeof(BlankPage1));
}
private void btnChangeColor_Click(object sender, RoutedEventArgs e)
{
ColorOfGrid.ColoreGenerale = new SolidColorBrush(Colors.Blue);
}
BlankPage1 XAML:
<Grid Background="LightSalmon">
<Grid.DataContext>
<local:ColorGridClass x:Name="ColorOfGrid" ColoreGenerale="Aquamarine"/>
</Grid.DataContext>
<TextBlock Text="Page1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,30,0,0"/>
<Grid Background="{Binding ColoreGenerale, Mode=OneWay}" HorizontalAlignment="Center" Height="500" Margin="0" VerticalAlignment="Center" Width="500">
<TextBlock Text="Grid Two" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,30,0,0"/>
</Grid>
</Grid>
如何使用數據綁定更改第二個網格的顏色?
在此先感謝。
你很可能達致這使用靜態資源或ThemeResource – Razor
你怎麼能舉個例子? – Res
https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/resourcedictionary-and-xaml-resource-references。這應該讓你開始 – Razor