【発行側】
public class class1
{
public delegate void _MyEvent(object sender, MyEventArgs e);
public event _MyEvent MyEvent;
protected virtual void MyChangeEvent(MyEventArgs e)
{
if ( MyEvent != null )
{
MyEvent( this, e );
}
}
private void Changed()
{
MyEventArgs e = new MyEventArgs( arg1, arg2 );
MyChangeEvent(e);
}
public class MyEventArgs : EventArgs
{
public string Arg1;
public int Arg2;
...
public MyEventArgs ( string _Arg1, int _Arg2 )
{
Arg1 = _Arg1;
Arg2 = _Arg2;
}
}
}
【受取側】
public class class2
{
private void Define()
{
class1 obj = new class1();
obj.MyEvent += new class1._MyEvent(RecieveEvent);
}
private void RecieveEvent( object sender, class1.MyEventArgs e)
{
Console.WriteLine(e.Arg1 + e.Arg2);
}
}
2016年3月14日月曜日
2016年3月8日火曜日
Wpf細かい書式
毎回忘れる細かい書式
・.Resource内での宣言
<.Resource>
<DataTemplate DataType="{x:Type [namespace]:[class]}" />
</.Resource>
・ListBoxへのBinding
要素が1つなら
<ListBox ItemsSource="{Binding [List]}" DisplayMemberPath="[Listの要素]"/>
要素が複数かつ色々変えるなら
<ListBox ItemsSource="{Binding [List]}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding [要素1]}"/>
<TextBlock Text="{Binding [要素2]}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
選択できなくて良いなら
<ItemsControl ItemsSource="{Binding [List]}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="LightPink" CornerRadius="20">
<TextBlock Margin="1">
<!--Run Text="要素1 : " FontSize="10"/-->
<Run Text="{Binding [要素1]}" FontSize="10"/>
<LineBreak/>
<Run Text="要素2 : "/>
<Run Text="{Binding [要素2]}"/>
</TextBlock>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
・PropertyChangedの通知
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
・.Resource内での宣言
<.Resource>
<DataTemplate DataType="{x:Type [namespace]:[class]}" />
</.Resource>
・ListBoxへのBinding
要素が1つなら
<ListBox ItemsSource="{Binding [List]}" DisplayMemberPath="[Listの要素]"/>
要素が複数かつ色々変えるなら
<ListBox ItemsSource="{Binding [List]}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding [要素1]}"/>
<TextBlock Text="{Binding [要素2]}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
選択できなくて良いなら
<ItemsControl ItemsSource="{Binding [List]}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="LightPink" CornerRadius="20">
<TextBlock Margin="1">
<!--Run Text="要素1 : " FontSize="10"/-->
<Run Text="{Binding [要素1]}" FontSize="10"/>
<LineBreak/>
<Run Text="要素2 : "/>
<Run Text="{Binding [要素2]}"/>
</TextBlock>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
・PropertyChangedの通知
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
PasswordBoxでデータバインド
【参考】http://garafu.blogspot.jp/2014/09/wcf-passwordbox.html
セキュリティの問題から基本的には非推奨
-----------------------
利用方法
<PasswordBox [namespace]:PasswordBoxHelper.Password = {Binding Target}/>
------------------------
【PasswordBoxHelper.cs】
セキュリティの問題から基本的には非推奨
-----------------------
利用方法
<PasswordBox [namespace]:PasswordBoxHelper.Password = {Binding Target}/>
------------------------
【PasswordBoxHelper.cs】
namespace WpfPasswordBox{ using System.Windows; using System.Windows.Controls; public class PasswordBoxHelper : DependencyObject { public static readonly DependencyProperty IsAttachedProperty = DependencyProperty.RegisterAttached( "IsAttached", typeof(bool), typeof(PasswordBoxHelper), new FrameworkPropertyMetadata(false, PasswordBoxHelper.IsAttachedProperty_Changed)); public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached( "Password", typeof(string), typeof(PasswordBoxHelper), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, PasswordBoxHelper.PasswordProperty_Changed)); public static bool GetIsAttached(DependencyObject dp) { return (bool)dp.GetValue(PasswordBoxHelper.IsAttachedProperty); } public static string GetPassword(DependencyObject dp) { return (string)dp.GetValue(PasswordBoxHelper.PasswordProperty); } public static void SetIsAttached(DependencyObject dp, bool value) { dp.SetValue(PasswordBoxHelper.IsAttachedProperty, value); } public static void SetPassword(DependencyObject dp, string value) { dp.SetValue(PasswordBoxHelper.PasswordProperty, value); } private static void IsAttachedProperty_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e) { var passwordBox = sender as PasswordBox; if ((bool)e.OldValue) { passwordBox.PasswordChanged -= PasswordBoxHelper.PasswordBox_PasswordChanged; } if ((bool)e.NewValue) { passwordBox.PasswordChanged += PasswordBoxHelper.PasswordBox_PasswordChanged; } } private static void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e) { var passwordBox = sender as PasswordBox; PasswordBoxHelper.SetPassword(passwordBox, passwordBox.Password); } private static void PasswordProperty_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e) { var passwordBox = sender as PasswordBox; var newPassword = (string)e.NewValue; if (!GetIsAttached(passwordBox)) { SetIsAttached(passwordBox, true); } if ((string.IsNullOrEmpty(passwordBox.Password) && string.IsNullOrEmpty(newPassword)) || passwordBox.Password == newPassword) { return; } passwordBox.PasswordChanged -= PasswordBoxHelper.PasswordBox_PasswordChanged; passwordBox.Password = newPassword; passwordBox.PasswordChanged += PasswordBoxHelper.PasswordBox_PasswordChanged; } }}
登録:
コメント (Atom)