gpt4 book ai didi

c# - 如何在 WPF(MVVM/ Entity Framework )中更新表单时显示从数据库到 TextBlock 的外键数据值?

转载 作者:行者123 更新时间:2023-12-03 10:42:25 25 4
gpt4 key购买 nike

OpcUaEndpoint with data

模式、描述在文本 block 中显示,但外键值 OpcUaEndpoint未显示。

我在这里想念什么?请帮忙。

Xaml 标记:

<ItemsControl Name="icName" Margin="10,10,10,10" ItemsSource="{Binding Scans}" Grid.IsSharedSizeScope="True" Grid.ColumnSpan="6" MouseDown="icName_MouseDown">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border x:Name="Border" Padding="8" BorderThickness="0 0 0 1" BorderBrush="{DynamicResource MaterialDesignDivider}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="Checkerz" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ToggleButton VerticalAlignment="Center" IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource MaterialDesignActionLightToggleButton}" Content="{Binding Code}" Grid.Column="0" />
<StackPanel Margin="30 0 0 0" Grid.Column="1">
<TextBlock FontWeight="Bold" Text="{Binding Name}" />
<TextBlock>
<Run Text="Mode: " />
<Run Text="{Binding Mode}" />
<Run Text=", " />
<Run Text="{Binding Description}" />
<Run Text="{Binding Scan.OpcUaEndpointRefID}" />
</TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Code behind:

public ControlCenterView()
{
InitializeComponent();
var viewmodel = new ScanListViewModel();
ScanList.DataContext = viewmodel;
}

Modelview

public DB.model.OpcUaEndpoint OpcUaEndpoint
{
get { return _opcUaEndpoint; }
set
{
_opcUaEndpoint = value;
OnPropertyChanged("OpcUaEndpoint");
}
}

public IEnumerable<DB.model.OpcUaEndpoint> OpcUaEndpointsValues
{
get { return GetOpcUaEndpoints(); }
}

public IEnumerable<DB.model.Scan> ScanValues
{
get { return GetScans(); }
}

private IList<DB.model.OpcUaEndpoint> GetOpcUaEndpoints()
{
_opcUaEndpointsNames = new List<DB.model.OpcUaEndpoint>();

foreach (var opcpointname in opcUaEndpointsService.GetAll())
{
_opcUaEndpointsNames.Add(opcpointname);
}

return _opcUaEndpointsNames;
}



//OpcUaEndpoint Model
public class OpcUaEndpoint : PlantObject
{
/// <summary>
/// Get or Sets the Identifier.
/// </summary>
[Key]
public int OpcUaEndpointId { get; set; }

/// <summary>
/// name of the OpcUaEndpoints.
/// </summary>
[MaxLength(80)]
public string Name { get; set; }

/// <summary>
/// OpcUaEndpoint.
/// </summary>
[MaxLength(255)]
public string Endpoint { get; set; }

/// <summary>
/// description of the OpcUaEndpoints
/// </summary>
[MaxLength(255)]
public string Description { get; set; }

/// <summary>
///UserName of the OpcUaEndpoints
/// </summary>
[MaxLength(80)]
public string Username { get; set; }

/// <summary>
/// Password of the OpcUaEndpoints
/// </summary>
[MaxLength(512)]
public string Password { get; set; }

/// <summary>
/// RequestTimeout of the OpcUaEndpoints
/// </summary>
public int RequestTimeout { get; set; }

public ICollection<Scan> Scans { get; set; }

[NotMapped]
public bool IsSelected { get; set; }

[NotMapped]
public string SearchData { get; set; }

public override string ToString()
{
return Name;
}
}

//Scan Model
//its OpcUaEndpointId key to foreign key OpcUaEndpointRefId in Scan Model.
//And Scan Is Binded to Item control in Xaml.
public class Scan : PlantObject
{

[Key]
public int Id { get; set; }


public int ScanId { get; set; }

/// <summary>
/// name of the scan Mode.
/// </summary>
[MaxLength(80)]
public string Name { get; set; }


[MaxLength(255)]
public string Description { get; set; }



public string Code { get; set; }

**/// <summary>
/// Id of the OPC-UA Endpoint for the Scan
/// </summary>
[ForeignKey("OpcUaEndpoint")]
public int OpcUaEndpointRefId { get; set; }**
/// <summary>
/// Foreign key to OpcUaEndPoint
///
/// </summary>
public OpcUaEndpoint OpcUaEndpoint { get; set; }

[MaxLength(255)]
public string DrivingTag { get; set; }

[MaxLength(80)]
public string Operator { get; set; }

[MaxLength(80)]
public string Value { get; set; }
[MaxLength(80)]
public string DataMode { get; set; }
[NotMapped]
public bool IsSelected { get; set; }

public bool IsEnable { get; set; }

public DateTime? StartedOn { get; set; }

public DateTime? StoppedOn { get; set; }


[MaxLength(80)]
public string Status { get; set; }
}

最佳答案

目前尚不清楚您的意图是引用 OpcUaEndpoint 中的属性还是 View 模型中的属性。我认为问题在于直接绑定(bind)到 OpcUaEndpoint 的实例不能转换为字符串。

试试这个:

<Run Text="{Binding OpcUaEndpointRefId}" />

或这个:
<Run Text="{Binding OpcUaEndpoint.OpcUaEndpointID}" />

关于c# - 如何在 WPF(MVVM/ Entity Framework )中更新表单时显示从数据库到 TextBlock 的外键数据值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52136596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com