价值不会传递给UserControl的网格项目



我正在尝试在一个值一个值并执行操作的网格中创建一个用户控件:

网格:

<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());
    }
}

,但消息框以0返回workid。我检查了项目源,它将正确的值传递给WorkID。

编辑:

    public MainWindow()
    {
        InitializeComponent();
        List<DataItem1> test = new List<DataItem1>();
        test.Add(new DataItem1()
        {
            WorkID = 292
        });
        Grid1.ItemsSource = test;
    }

在构建UserControl期间不会发生绑定,因此不会在该阶段设置WorkID,而是在以后进行。您可以从Loaded事件中打印您的消息,在哪个阶段绑定已经发生。另外,您正在更改构造函数中的DataContext,这可能会引起问题,因此只需将其删除即可。然后,您的构造函数应该看起来像这样:

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

另外可能引起问题的一件事是您的依赖性属性未正确定义。定义依赖关系属性时,您必须遵循" propertyNameProperty"命名约定,您也应该与您的属性名称保持一致(即不要将WorkIdWorkID混合)

因此,请记住这些内容,您的财产声明应该看起来像:

public static readonly DependencyProperty WorkIDProperty = 
DependencyProperty.Register("WorkID", typeof(int), typeof(ActionNotes));
public int WorkID
{
    get { return (int)GetValue(WorkIDProperty); }
    set { SetValue(WorkIDProperty, value); }
}

我认为将依赖项属性放置在整数数据类型中的字符串值可能是一个问题。

我假设您在 actionNotes 的构造函数中调用 MessageBox.Show()用于调试目的,您不打算将该代码留在生产代码中。(:>)

无论如何,在构造函数的点上调用MessageBox.Show方法会导致 System.invalidoperationException 被抛出。

我相信,这种例外是为了阻止WPF的一些棘手的重新进入问题。

dispatcher.invokeasync 中,将调用呼叫呼叫,以允许调度程序解决问题。在这一点上,我能够看到Workid值。

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

另外,构造函数尚未设置WorkID的值。

fwiw,您可以在没有消息框的情况下进行调试,在构造函数中放置一个断点,然后在手表窗口或直接窗口中评估workid。

也没有显示XAML的actionnotes。

需要此一点XAML才能查看ActionNotes中的值。

<!-- in ActionNotes.xaml -->

最新更新