2017-09-15 54 views
1

我在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> 

如何使用數據綁定更改第二個網格的顏色?

在此先感謝。

+0

你很可能達致這使用靜態資源或ThemeResource – Razor

+0

你怎麼能舉個例子? – Res

+0

https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/resourcedictionary-and-xaml-resource-references。這應該讓你開始 – Razor

回答

2

在這種情況下,如果您想保持應用程序的一致性,您可以使用Application.Resources並創建SolidColorBrush並使用它來綁定顏色/在需要的地方更改顏色。

在App.xaml中創建Application.Resources並添加一個SolidColorBrush,您要將其用作默認Background。在我的情況下,我想使用Red

<Application.Resources> 
    <SolidColorBrush x:Key="GridColorSolidBrush" Color="Red" /> 
</Application.Resources> 

現在改變你的Mainpage.xamlBlankPage.xaml電網Background這個資源Key。如下所示。

<Grid Background="{StaticResource GridColorSolidBrush}"> 

現在在您的Button.Tapped事件中,您可以更改顏色,如下所示。

(Application.Current.Resources["GridColorSolidBrush"] as SolidColorBrush).Color = Colors.Green; 

這將改變所有FrameworkElements的顏色(在這種情況下Grid)。

Here你可以找到一個簡單的GitHub回購。

下面是我的示例程序的輸出。

enter image description here

+1

不錯!謝謝..完美.. – Res

相關問題