You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF字典数据绑定至DataGrid问题求助

WPF DataGrid展示解析结果问题

我正在开发一个WPF程序,功能是接收字符串,解析后存入字典(键为列标题,值为对应列内容)。解析完成后弹出第二个WPF窗口,通过DataGrid展示解析结果。我查过Stack Overflow的相关方案,但都不适用我的场景。

MainWindow代码

public MainWindow()
{
    
    InitializeComponent();
    DataContext = this;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    string input = HelloTextBox.Text;

    IMessage message = parseMessage(input);

    Type messageType = message.GetType();

    PropertyList proplist = GetPropertyList(messageType, message);

    // display message properties in popup window
    InfoDialog infoPopUp = new(proplist);
    infoPopUp.ShowDialog();

}

弹窗窗口代码

public partial class InfoDialog : Window
{
    
    public PropertyList PropertyList { get; set; }
    public InfoDialog(PropertyList propList)
    {
        InitializeComponent();
        this.PropertyList = propList;
        
    }

XAML代码

<Grid>
    ItemsSource="{Binding PropertyList,
          RelativeSource={RelativeSource AncestorType=Window}}" AutoGenerateColumns="False" SelectionChanged="DataGridXAML_SelectionChanged">
        <DataGrid.Columns>
            <!-- Header Text and Bindings -->
            <DataGridTextColumn Header="Key" Binding="{Binding Key}" Width="*"/>
            <DataGridTextColumn Header="Value" Binding="{Binding Value}"  Width="*"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

内容的提问来源于stack exchange,提问作者shayanvalaie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 20:25:22