忍者ブログ
2009/12/12作成
  • « 2024.05.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • »
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

[WPF]DataGridの指定セルにフォーカスする
[WPF]ViewModelを介してDataGridにバインディングするにボタンを一つ配置し、以下のようなクリックハンドラとメソッドを作る。

private void button1_Click(object sender, RoutedEventArgs e)
{
    int rowIndex = 0;
    int colIndex = 1;
    // 行、列の番号を指定してDataGridCellを取得する
    DataGridCell cell = GetDataGridCell(dataGrid1, rowIndex, colIndex);
    // データグリッドセルにフォーカスする
    FocusManager.SetFocusedElement(this, cell);
}

public DataGridCell GetDataGridCell(DataGrid dataGrid, int rowIndex, int columnIndex)
{
    if (dataGrid.Items == null || dataGrid.Items.IsEmpty)
    {
        return null;
    }
    DataGridRow row = GetDataGridRow(dataGrid, rowIndex);
    if (row != null)
    {
        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
        DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
        if (cell == null)
        {
            // Visual Treeが構築されていない場合もあるため更新
            dataGrid.UpdateLayout();
            dataGrid.ScrollIntoView(row, dataGrid.Columns[columnIndex]);
            cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
        }
        return cell;
    }
    return null;
}

public DataGridRow GetDataGridRow(DataGrid dataGrid, int index)
{
    if (dataGrid.Items == null || dataGrid.Items.IsEmpty)
    {
        return null;
    }
    DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
    if (row == null)
    {
        // Visual Treeが構築されていない場合もあるため更新
        dataGrid.UpdateLayout();
        dataGrid.ScrollIntoView(dataGrid.Items[index]);
        row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
    }
    return row;
}

public static T GetVisualChild<T>(Visual parent) where T : Visual
{
    T result = default(T);
    int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < childrenCount; i++)
    {
        Visual child = (Visual)VisualTreeHelper.GetChild(parent, i);
        result = child as T;
        if (result == null)
        {
            result = GetVisualChild<T>(child);
        }
        if (result != null)
        {
            break;
        }
    }
    return result;
}


PR
[WPF]ViewModelを介してDataGridにバインディングする
○XAML
赤字で、ViewModelのプロパティと紐付けを行う。
青字・緑時で、赤字で紐付けしたプロパティ(DataTable)の列名と紐付けを行う。

<Window x:Class="WpfDataGrid.BindingWithViewModel.TestWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TestWindow" Height="300" Width="300" Loaded="Window_Loaded">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left"
                  Name="dataGrid1" VerticalAlignment="Top" ItemsSource="{Binding DataTable}">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="Column1" Binding="{Binding Path=col1}" Width="100" />
                <DataGridTextColumn x:Name="Column2" Binding="{Binding Path=col2}" Width="100" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>



○ViewModel

using System.Data;

namespace WpfDataGrid.BindingWithViewModel
{
    class ViewModel
    {
        public DataTable DataTable { get; set; }
    }
}



○コードビハインド
水色でViewModelを設定する。

using System.Windows;
using System.Data;

namespace WpfDataGrid.BindingWithViewModel
{
    /// 
    /// TestWindow.xaml の相互作用ロジック
    /// 

    public partial class TestWindow : Window
    {
        public TestWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("col1");
            dt.Columns.Add("col2");
            dt.Rows.Add("1", "One");
            dt.Rows.Add("2", "Two");
            ViewModel vm = new ViewModel();
            vm.DataTable = dt;
            DataContext = vm;
        }
    }
}


[WPF] KeyDownをプログラムから呼び出す
○WPFプロジェクト内から呼び出す場合

Dim key As Key = Key.Insert
Dim target As UIElement = Keyboard.FocusedElement
Dim routedEvent As RoutedEvent = Keyboard.KeyDownEvent
Dim e As KeyEventArgs = New KeyEventArgs(
                            Keyboard.PrimaryDevice,
                            PresentationSource.FromVisual(target),
                            0,
                            key)
e.RoutedEvent = routedEvent
Grid1.RaiseEvent(e)



○単体テストプロジェクト内から呼び出す場合

Dim params As HwndSourceParameters = New HwndSourceParameters()
Dim source As HwndSource = New HwndSource(params)
Dim k As Key = Key.A
Dim e As KeyEventArgs = New KeyEventArgs(Keyboard.PrimaryDevice, source, 0, k)
e.RoutedEvent = routedEvent
Grid1.RaiseEvent(e)


CLaunch
デスクトップをすっきりしたい人におすすめのランチャーアプリ「CLaunch」。

テストのエビデンスをとるときに汚いデスクトップを見せなくてすむよう、
普段から整理するのに便利です。

個人的に好きな設定
CLaunch.iniファイルダウンロード
Design.iniファイルダウンロード
ClickOnce

ClickOnce


「Webアプリのメリットは、バージョンアップはサーバーサイドのみでよい。」
「それに比べ、クラサバではバージョンアップのたび再インストールが必要。」
と比較することで、Webアプリのメリットを示したことがある。

が、ClickOnceは、クラサバのデメリットをほとんど無視できる機能だと思った。
つまり再インストールをワンクリックでできるようにする。
インストーラを配信するサイトを自動生成したり、
アプリケーション起動時に、バージョンアップを促すこともできるみたい。

参考文献⇒7. アプリケーションの配置
Powered by [PR]
/ Design by sky hine / PR:忍者ブログ