グリッドの背景をr、g、b値で0.5秒ごとに変更し、グリッド内のボタンとやりとりできるようにします。 問題は、定期的な間隔で背景が変わることも、グリッド内の2つのボタンと対話することもできないことです。グリッドの色は、他のアプリケーションまたはタスクマネージャに切り替えるときにのみ変化します。達成する方法?WPF:グリッドのバックグラウンドを半分ごとに変更しても、他のコントロールと相互作用します。
<Grid Name="MainGrid" Height="{Binding ElementName=MainWindow1, Path=ActualHeight}" Loaded="MainGrid_Loaded">
<StackPanel VerticalAlignment="Center">
<Label Height="50" Name="xCoordinate" />
<Label Height="50" Name="yCoordinate" />
<StackPanel HorizontalAlignment="Center" Height="50" Orientation="Horizontal">
<Button Content="Red" Width="100" Name="xBtn" Click="xBtn_Click" Margin="0,0,10,0"/>
<Button Content="Blue" Width="100" Name="yBtn" Click="yBtn_Click" />
</StackPanel>
</StackPanel>
</Grid>
public partial class MainWindow : Window
{
Thread thread;
public MainWindow()
{
InitializeComponent();
thread = new Thread(new ThreadStart(ChangeGridColor));
thread.Start();
}
private void xBtn_Click(object sender, RoutedEventArgs e)
{
xCoordinate.Background = new SolidColorBrush(Colors.Red);
}
private void yBtn_Click(object sender, RoutedEventArgs e)
{
yCoordinate.Background = new SolidColorBrush(Colors.Blue);
}
private void MainWindow_MouseMove(object sender, MouseEventArgs e)
{
Point point = Mouse.GetPosition(Application.Current.MainWindow);
xCoordinate.Content = point.X;
yCoordinate.Content = point.Y;
}
byte r=0,g=0,b = 0;
public void ChangeGridColor()
{
while (true)
{
this.Dispatcher.Invoke((Action)(() =>
{//this refer to form in WPF application
MainGrid.Background = new SolidColorBrush(Color.FromRgb(r, g, b));
r += 1;
g += 1;
b += 1;
}));
Thread.Sleep(1000);
}
}
}
コードでそれを行うのはなぜ? XAMLの数行でWPFのストーリーボードを使用する – MickyD