私はxamlが初めてです。私はかなり見回しましたが、私がやろうとしていることに近い投稿を見つけていますが、探しているものを正確に見つけることができませんでした.
依存関係プロパティを持つ外部 WPF UserControl があります。別のプロジェクトでは、UserControl ライブラリから dll をインポートしました。各セルにユーザーコントロールのインスタンスがあるプロジェクトに静的グリッドを作成しました。私が定義した各 UserControl DependencyProperty は xaml で利用できます。
実行時に、DataTable にデータを入力し、グリッド内の UserControls を DataTable にバインドして、各 UserControl が DataTable の値を反映するようにします。DataTable の各行には、UserControl の依存関係プロパティにバインドする必要があるいくつかの列値があります。
私が問題を抱えているのは、データが取得された後に DataContext を定義して、各 UserControls の Dependency プロパティのバインディング パスを定義できるようにすることです。次のように、グリッドの DataContext を DataTable に設定すると:
Grid.DataContext = myDataTable;
では、UserControl プロパティのバインド パスの構文は何ですか?
<my:ucControl Name="myUserControl1" MyDependencyProp="{Binding Path=??}" Grid.Column="1" Grid.Row="1" />
「Row[0][column_name]」またはそのバリエーションを使用した投稿を見たことがありますが、そのようなものは機能していないようです。バインド方法が間違っていますか?
ここに私のUserControlコードがあります:
public partial class ucControl : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public static DependencyProperty IndexProperty =
DependencyProperty.Register("Index", typeof(int), typeof(ucControl));
public static DependencyProperty StatusProperty =
DependencyProperty.Register("Status", typeof(String), typeof(ucControl),
new PropertyMetadata(string.Empty, new PropertyChangedCallback(NotifyPropertyChanged)));
#region PublicProperties
public int Index
{
get { return (int)GetValue(IndexProperty); }
set { SetValue(IndexProperty, value); }
}
public String Status
{
get { return (int)GetValue(StatusProperty); }
set { SetValue(StatusProperty, value); }
}
#endregion
}
XAML は次のとおりです。
<Grid Canvas.Left="17" Canvas.Top="46" Name="MasterGrid" Width="1259" HorizontalAlignment="Left" VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition Name="ColumnHeader" Height="25" />
<RowDefinition Height="40" />
<RowDefinition Height="40" />
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Name="DateColumn" Width="80"/>
<ColumnDefinition Width="65"/>
<ColumnDefinition Width="65"/>
<ColumnDefinition Width="65"/>
</Grid.ColumnDefinitions>
<my:ucControl Name="myControl1" Index="{Binding ??}" Status="{Binding ??}" Grid.Column="1" Grid.Row="1" Grid.RowSpan="1" />
<my:ucControl Name="myControl2" Index="{Binding ??}" Status="{Binding ??}" Grid.Column="1" Grid.Row="2" Grid.RowSpan="1" />
<my:ucControl Name="myControl3" Index="{Binding ??}" Status="{Binding ??}" Grid.Column="1" Grid.Row="2" Grid.RowSpan="1" />
</Grid>
ここに私のC#があります:
String strViewSelect = "SELECT index, status, FROM Table1 ORDER BY index ASC;
System.Data.DataSet dsMonthData = dataClass.mFillDataset(strViewSelect);
MasterGrid.DataContext = dsMonthData;
更新: この問題を解決した方法は次のとおりです。
まず、バインド先の列を含むプロジェクトに静的 DataSource を作成しています。クエリに引数を追加して、取得するデータを指定できるようにします。
ウィンドウの XAML に次のものが追加されました。
<Window.Resources>
<my1:myDataSet x:Key="myDataSet" />
<CollectionViewSource x:Key="tableNameViewSource" Source="{Binding Path=tableName, Source={StaticResource myDataSet}}" />
</Window.Resources>
次に、次の XAML をグリッドに追加しました。
DataContext="{StaticResource tableNameViewSource}"
これで、バインディングを UserControl プロパティに追加できます。
Status="{Binding Path=statusColumn}"
<my:ucCalendarCell Name="ucControl1" Status="{Binding Path=statusColumnName}" Index="{Binding Path=indexColumnName}" Grid.Column="1" Grid.Row="1" Grid.RowSpan="1" />
私のニーズに合わせるための作業が少し残っていますが、それが基本的な考え方です。これを行うには複数の方法があると確信していますが、これは私にとってはうまくいくはずです。