我想創建一個網格,取一個值,並執行一個動作中的用戶控件:價值沒有被傳遞給用戶控件電網的ItemSource
網:
<DataGrid x:Name="Grid1" CanUserAddRows="False" AutoGenerateColumns="False"
CanUserReorderColumns="False" CanUserResizeColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="False" ScrollViewer.CanContentScroll="True"
SelectionUnit="Cell"
ClipboardCopyMode="None" HeadersVisibility="Column"
HorizontalScrollBarVisibility="Disabled"
Width="1200" Height="150" BorderThickness="0"
GridLinesVisibility="None" BorderBrush="{x:Null}">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="Black" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Background>
<SolidColorBrush Color="White"/>
</DataGrid.Background>
<DataGrid.ItemBindingGroup>
<BindingGroup/>
</DataGrid.ItemBindingGroup>
<DataGrid.Columns>
<DataGridTemplateColumn CanUserResize="False"
Header="Action Notes"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:ActionNotes WorkID="{Binding WorkID}">
</local:ActionNotes>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
用戶控制代碼:
public partial class ActionNotes : UserControl
{
public static readonly DependencyProperty WorkIdDependency =
DependencyProperty.Register("WorkID", typeof(int), typeof(ActionNotes));
public int WorkID
{
get { return (int)GetValue(WorkIdDependency); }
set { SetValue(WorkIdDependency, value); }
}
public ActionNotes()
{
InitializeComponent();
this.DataContext = this;
MessageBox.Show(this.WorkID.ToString());
}
}
但MessageBox返回WorkID爲0. 我檢查了項目源,並且它將正確的值傳遞給WorkID。
編輯:放置在依賴屬性
public MainWindow()
{
InitializeComponent();
List<DataItem1> test = new List<DataItem1>();
test.Add(new DataItem1()
{
WorkID = 292
});
Grid1.ItemsSource = test;
}
什麼是爲Datagrid中的ItemSource ... – Sankarann
的是的ItemSource列表<>。我可以迭代Grid.Items並查看所有正確的值。 – Arm0geddon