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

CanExecuteのTrue,Falseを変更してもコマンド実行の可否がGUIに反映されない場合には

2011年4月12日 火曜日

CommandManager.InvalidateRequerySuggested();
を実行して、RequerySuggestedイベントを強制的に発生させてCanExecuteが実行されるようにできる。

UIスレッドと別スレッドから呼び出すときはDispatcher.Invoke()やDispatcher.BeginInvokeのデリゲートから呼び出す。

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をセットする。