0

我正在尝试将以下 xaml 代码转换为过程代码。

                <docking:ToolWindow Title="{Binding ElementName=errorListView, Path=Items.Count, StringFormat={}Error List ({0}), FallbackValue=Error List}" >
                    <ListView x:Name="errorListView" BorderThickness="0">
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Line" Width="40" DisplayMemberBinding="{Binding Path=PositionRange.StartPosition.DisplayLine}" />
                                <GridViewColumn Header="Char" Width="40" DisplayMemberBinding="{Binding Path=PositionRange.StartPosition.DisplayCharacter}" />
                                <GridViewColumn Header="Description" Width="300" DisplayMemberBinding="{Binding Path=Description}" />
                            </GridView>
                        </ListView.View>
                    </ListView>
                </docking:ToolWindow>

但是我无法为 ToolWindow 的 Title 和 GridViewColumn 的 DisplayMemberBinding 做绑定。我想知道如何访问 PositionRange.StartPosition.DisplayLine 进行绑定。

以下是我的尝试。

        var toolWindow = new ToolWindow();

        var errorlistView = new ListView { Name = "errorListView", BorderThickness = new Thickness(0)};

        var gridView = new GridView();
        var gridViewColumnLine = new GridViewColumn();
        gridViewColumnLine.Header = "Line";
        gridViewColumnLine.Width = 40;
        var bindingDisplayLine = new Binding();
        bindingDisplayLine.Path = new PropertyPath("PositionRange.StartPosition.DisplayLine");
     -->BindingOperations.SetBinding(gridViewColumnLine, );
        gridView.Columns.Add(gridViewColumnLine);

        var gridViewColumnChar = new GridViewColumn();
        gridViewColumnChar.Header = "Char";
        gridViewColumnChar.Width = 40;
        var bindingDisplayChar = new Binding();
        bindingDisplayChar.Path = new PropertyPath("PositionRange.StartPosition.DisplayCharacter");
     -->BindingOperations.SetBinding(gridViewColumnChar, );
        gridView.Columns.Add(gridViewColumnChar);

        var gridViewColumnDescription = new GridViewColumn();
        gridViewColumnDescription.Header = "Description";
        gridViewColumnLine.Width = 40;
        var bindingDescription = new Binding();
        bindingDescription.Path = new PropertyPath("Description");
     -->BindingOperations.SetBinding(gridViewColumnDescription, )
        gridView.Columns.Add(gridViewColumnDescription);

        errorlistView.View = gridView;

        var bnd = new Binding();
        bnd.Source = errorlistView;
        bnd.Path = new PropertyPath("Items.Count");
     -->bnd.StringFormat = String.Format("Error List ({0})");
        bnd.FallbackValue = "Error List";
        BindingOperations.SetBinding(toolWindow, ToolWindow.TitleProperty, bnd);
4

1 回答 1

0

DisplayMemberBinding属性不是依赖属性,因此不能使用 SetBinding。相反,它已经是 BindingBase 类型,因此您可以直接分配绑定对象:

gridViewColumnLine.DisplayMemberBinding = bindingDisplayLine;

我想您还必须指定绑定的源或 GridViewColumn 的DataContext

于 2012-12-08T13:01:57.457 回答