‘WPF’ カテゴリーのアーカイブ

WPFで長時間かかる処理の間にUIのイベントを処理させるには

2010年7月11日 日曜日

WPFでWindows FormのApplication.DoEventsメソッドと同様に長時間かかる処理の間にUIのイベントを処理させるには、下記のように行う。(MSDNより)

[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public void DoEvents()
{
    DispatcherFrame frame = new DispatcherFrame();
    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
        new DispatcherOperationCallback(ExitFrame), frame);
    Dispatcher.PushFrame(frame);
}

public object ExitFrame(object f)
{
    ((DispatcherFrame)f).Continue = false;

    return null;
}

WPF でテーマを使用するには

2010年3月4日 木曜日

WPF.Themesを使ってWPFでSilverlight ToolkitのようなThemeを使用することができる。現状21種類のテーマが提供されている。

  1. WPF.Themes.dllWPFToolkit.dllを入手しそれぞれのDLLの参照を追加する。
  2. Windowタグに下記太字部分を追加する。(ThemeにShinyRedを使用する場合)

<Window x:Class=”WpfApplication1.Window1″
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml
            xmlns:themes=”clr-namespace:WPF.Themes;assembly=WPF.Themes”
            themes:ThemeManager.Theme=”ShinyRed”
    Title=”Window1″ Height=”300″ Width=”300″>

WPF版WebBrowserコントロール

2009年9月12日 土曜日

.Net framework 3.5 SP1からWPFにもWebBrowserコントロールが追加された。

WebBrowserコントロールのDocumentプロパティは型objectとなっているが、COM参照「Microsoft Html Object Library」を追加すると、mshtml.HTMLDocumentにキャスト可能となり、HTMLドキュメントの操作等が可能となる。

WPFでScrollViewerで特定のコントロールの位置までスクロールを行うには

2009年1月27日 火曜日

Page(Window)ロード時にスクロールなしの状態のコントロールの座標を取得しておく。

scrollY = targetControl.TranslatePoint(new Point(0, 0), scrollviewer1).Y;

下記を実行してスクロールを行う。

scrollviewer1.ScrollToVerticalOffset(scrollY );

WPFでキーボードフォーカスを変更できないようにするには

2008年11月18日 火曜日

PreviewLostKeyboardFocusイベントでKeyboardFocusChangedEventArgsのHandledプロパティをtrueにする。

WPFでApplicationCommandsを使用する

2008年11月10日 月曜日

ButtonのCommandプロパティをセットする。下記は組み込みのApplicationCommands.Saveを使用する場合。

<Button Height=”23″ Name=”button1″ Width=”75″ Command=”ApplicationCommands.Save”>Button</Button>

Xamlのコードビハインドクラスのコンストラクタで、commandBindingを作成し、ExecutedイベントとCanExecuteイベントを登録し、CommandBindingsプロパティにcommandBindingを追加する。

CommandBinding binding = new CommandBinding(ApplicationCommands.Save);
binding.Executed += new ExecutedRoutedEventHandler(executeMethod);
binding.CanExecute += new CanExecuteRoutedEventHandler(binding_CanExecute);
this.CommandBindings.Add(binding);

Executedイベントはコマンドでするべき処理を実行する。CanExecuteイベントはコントロールがコマンドを実行できるかどうか確認するときに呼び出す。コマンドが実行できる状態の場合は、

void binding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
のようにパラメータのCanExecuteプロパティにtrueをセットする。

WPFでバリデーションを設定する手順

2008年7月31日 木曜日

TextBox.Textに設定する手順。下記のようにValidation.ErrorTemplateにバリデーションを表示するコントロールのテンプレートを設定し、Binding.ValidationRulesプロパティにExceptionValidationRuleを設定する。

ExceptionValidationRuleはバインディング ソース プロパティの更新中にスローされる例外をチェックできる。このほかにWindows Formsで使われるIDataErrorInfoに夜エラーをチェックできるDataErrorValidationRuleも使用できる。

<TextBox Width="60" TextAlignment="Right" TabIndex="1" Validation.ErrorTemplate="{StaticResource ValidationTemplate}"
Style="{ StaticResource TextBoxInError}" >
<TextBox.Text>
<Binding Path="RecordCount" Mode="TwoWay">
<Binding.ValidationRules>
<ExceptionValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>

バリデーションを表示するコントロールのテンプレートの設定例は下記。

<ControlTemplate x:Key="ValidationTemplate">
<DockPanel>
<AdornedElementPlaceholder/>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
</DockPanel>
</ControlTemplate>

AdornedElementPlaceholderはこの場合TextBoxの位置を表し、この例ではTextBoxの後に「!」を表示する。

さらに下記の設定で例外のメッセージをTextBoxのToolTipとして表示できる。

<Style x:Key="TextBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>