Created
December 24, 2014 22:43
-
-
Save devlights/512435fe6696f5ceee18 to your computer and use it in GitHub Desktop.
WPFでWinFormsのButtonのPerformClickと同じようなことを実施
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows; | |
namespace WpfButtonPerformClick.Extensions | |
{ | |
public static class FrameworkElementExtensions | |
{ | |
public static T FindByName<T>(this FrameworkElement self, string name) where T : FrameworkElement | |
{ | |
return self.FindName(name) as T; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="WpfButtonPerformClick.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:sys="clr-namespace:System;assembly=mscorlib" | |
Title="MainWindow" | |
Height="350" | |
Width="525"> | |
<Window.DataContext> | |
<sys:String>hello world</sys:String> | |
</Window.DataContext> | |
<Grid> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="Auto" /> | |
<RowDefinition /> | |
</Grid.RowDefinitions> | |
<StackPanel Grid.Row="0" Orientation="Horizontal"> | |
<StackPanel.Resources> | |
<Style TargetType="Button"> | |
<Setter Property="Margin" Value="10" /> | |
<Setter Property="Height" Value="40" /> | |
<Setter Property="Width" Value="100" /> | |
</Style> | |
</StackPanel.Resources> | |
<Button Name="btn1" Content="Button 1" Click="btn1_Click" /> | |
<Button Name="btn2" Content="Button 2" Click="btn2_Click" /> | |
</StackPanel> | |
<DockPanel Grid.Row="1" LastChildFill="True"> | |
<TextBlock Text="{Binding Path=.}" FontSize="40" TextAlignment="Center" VerticalAlignment="Center" /> | |
</DockPanel> | |
</Grid> | |
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WpfButtonPerformClick | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Windows; | |
using System.Windows.Controls; | |
using WpfButtonPerformClick.Extensions; | |
/// <summary> | |
/// MainWindow.xaml の相互作用ロジック | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
private void btn1_Click(object sender, RoutedEventArgs e) | |
{ | |
this.FindByName<Button>("btn2").PerformClick(); | |
} | |
private void btn2_Click(object sender, RoutedEventArgs e) | |
{ | |
DataContext = "Button 2 Clicked."; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment