xaml绑定不显示视图模型

sf6xfgos  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(151)

我尝试在xaml中显示数据,并尝试了数据网格和列表视图。服务按预期收集数据。DataContextMap到视图- debug显示数据。DataGrid中的绑定不显示任何数据。
视图模型为:

public class BasePointViewModel
{
    public BasePointElement BasePointElement;
    public BasePointViewModel(BasePointService basePointService)
    {
        this.BasePointElement = basePointService.GetBasePoints();
    }
}

将视图模型Map到视图的basePointCommand为:

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        try
        {
            var uiApp = commandData.Application;
            BasePointService bps = new(uiApp);
            BasePointViewModel vm = new(bps);
            BasePointView v = new()
            {
                DataContext = vm,
            };
            v.ShowDialog();
        }
        catch (Exception ex)
        {
            TaskDialog.Show("Errors", ex.ToString());
        }
        return Result.Succeeded;
    }

DataContext = vm返回预期结果x1c 0d1x
这绑定在这xaml是不再工作.这datagrid是空的除了这标题.

Window x:Class="DevTech.BasePointButton.BasePointView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DevTech.BasePointButton"
    mc:Ignorable="d"
    Title="BasePointView" SizeToContent="Height" Width="500">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="30"/>
    </Grid.RowDefinitions>
    <DataGrid
        ItemsSource="{Binding Path=BasePointElement, Mode=OneWay}"
        AutoGenerateColumns="False"
        VerticalScrollBarVisibility="Auto"
        HorizontalScrollBarVisibility="Hidden"
        CanUserAddRows="False"
        CanUserSortColumns="True"
        CanUserResizeColumns="True"
        IsReadOnly="False"
        SelectionMode="Extended"
        SelectionUnit="FullRow"
        MaxHeight="400"
        Margin="10"
        BorderThickness="0">
        <DataGrid.Columns>
            <DataGridTextColumn
                Header="Project Base Point"
                Binding="{Binding ProjBasePtEw}"
                Width="150"
                IsReadOnly="True"
            />
        </DataGrid.Columns>
      </DataGrid>
</Grid>

装订线哪里坏了?谢谢你的帮助!

4zcjmb1e

4zcjmb1e1#

我可以让数据显示在一个堆栈面板中,如下所示-为什么在数据网格中不起作用呢?

<Window x:Class="DevTech.BasePointButton.BasePointView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="BasePointView" SizeToContent="Height" Width="500">
<Grid>
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Width="110" Margin="10, 0, 0, 0"></Label>
            <Label Width="100" Margin="10, 0, 0, 0">EW:</Label>
            <Label Width="100" Margin="10, 0, 0, 0" FontWeight="DemiBold" HorizontalAlignment="Left">EL</Label>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Width="110" Margin="10, 0, 0, 0">Project Base Point :</Label>
            <TextBlock Text="{Binding BasePointElement.ProjBasePtEw, Mode=OneWay}" Margin="10, 1, 0, 0" Width="100"/>
        </StackPanel>
    </StackPanel>
</Grid>

相关问题