我目前有一個 WPF,它接收一個字串,對其進行決議并將其存盤在一個字典中,其中鍵是列標題,值在它下面。決議字串后,將打開第二個 WPF 彈出視窗,其中包含應顯示此已決議訊息的資料網格。我查看了 Stack Overflow 以查看遇到此問題的其他人,但他們的解決方案都不適用于我。
主視窗
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>
uj5u.com熱心網友回復:
您忘記設定系結的源物件:
ItemsSource="{Binding PropertyList,
RelativeSource={RelativeSource AncestorType=Window}}"
或者,設定
DataContext = this;
在視窗的建構式中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/516092.html
標籤:C#。网wpf数据绑定
