2014-01-30 69 views
0

我想創建一個網格,取一個值,並執行一個動作中的用戶控件:價值沒有被傳遞給用戶控件電網的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; 
    } 
+0

什麼是爲Datagrid中的ItemSource ... – Sankarann

+0

的是的ItemSource列表<>。我可以迭代Grid.Items並查看所有正確的值。 – Arm0geddon

回答

2

綁定你的用戶控件,以便WorkID不會在該階段被設置在建設過程中不發生,但後來才。您可以打印來自Loaded事件的消息,在該階段該綁定應該已經發生。另外,您正在更改構造函數中的DataContext,這可能會導致問題,因此只需將其刪除即可。那麼你的構造應該是這樣的:

很可能會導致問題
public ActionNotes() 
{ 
    InitializeComponent(); 

    Loaded += (sender, e) => MessageBox.Show(this.WorkID.ToString()); 
} 

一件事是,你的依賴屬性沒有正確定義。在定義依賴項屬性時,您必須遵循「PropertyNameProperty」命名約定,並且還應該與您的屬性名稱保持一致。不WorkID混合WorkId

所以保持這些想法,你的財產申報應該是這樣的:

public static readonly DependencyProperty WorkIDProperty = 
DependencyProperty.Register("WorkID", typeof(int), typeof(ActionNotes)); 

public int WorkID 
{ 
    get { return (int)GetValue(WorkIDProperty); } 
    set { SetValue(WorkIDProperty, value); } 
} 
0

我覺得值具有整數數據類型,這可能是一個問題的字符串值。

+0

我剛剛將數據類型更改爲字符串。現在這個值是空字符串。 – Arm0geddon

+0

@ Arm0geddon在哪裏設置了ItemsSource? – Dilmah

+0

ItemSource在構造函數中設置。在ItemSource被設置後,我可以在遍歷Grid.Items之後看到所有的值。該值只是不會使其成爲UserControl。 – Arm0geddon

0

我假設你叫MessageBox.Show()ActionNotes調試目的的構造函數,你不打算離開,在生產代碼。 (:>)

無論如何,在構造函數中調用MessageBox.Show方法會導致引發System.InvalidOperationException異常

我相信這個異常是拋出來阻止WPF一些棘手的重入問題。

將呼叫調用到Dispatcher.InvokeAsync中的Show方法以允許Dispatcher解決問題。此時,我可以看到WorkID值。

public ActionNotes() { 
     InitializeComponent(); 
     var x = this.WorkID.ToString(); 
     Dispatcher.InvokeAsync(() => 
     { 
     MessageBox.Show(this.WorkID.ToString()); 
     }); 
} 

此外,WorkID的值尚未在構造函數中設置。

FWIW,你可以在沒有MessageBox的情況下進行調試,在構造函數中放置一個斷點並在Watch Window或Immediate Window中評估this.WorkID。

此外,您還沒有顯示ActionNotes的XAML。

需要這一點XAML來查看ActionNotes中的值。

<!-- in ActionNotes.xaml -->