[英]Binding: how can I bind a Property from another class
I am trying to bind the property isfavorit
from the table bewertungen
, which is an ICollection in the class filme_serien
.我正在尝试从表
bewertungen
绑定属性isfavorit
,这是 class filme_serien
中的 ICollection 。 I've got a List of filme_serien
.我有一个
filme_serien
的列表。 And I am trying to bind the isfavorit
form the bewertungen
table through the filme_serien
List.我正在尝试通过
filme_serien
列表绑定isfavorit
表中的bewertungen
。
ER:急诊室:
C#: C#:
filme_serien:电影连续剧:
public partial class FilmeSerien
{
public FilmeSerien()
{
Bewertungens = new HashSet<Bewertungen>();
}
public int FId { get; set; }
public bool Isfilm { get; set; }
public virtual ICollection<Bewertungen> Bewertungens { get; set; }
}
Bewertungen:贝维尔通根:
public partial class Bewertungen
{
public string BwAcc { get; set; }
public int BwFs { get; set; }
public bool Isfavorit { get; set; }
public int Bewertung { get; set; }
public virtual Account BwAccNavigation { get; set; }
public virtual FilmeSerien BwFsNavigation { get; set; }
}
The FSList
and SelectedFS
are in the FSViewModel
: FSList
和SelectedFS
在FSViewModel
中:
public IEnumerable<FilmeSerien> FSList
{
get => _db?.FilmeSeriens.Include(x => x.Bewertungens).AsNoTracking().ToList();
};
public FilmeSerien _selectedFS;
public FilmeSerien SelectedFS
{
get => _selectedFS;
set
{
_selectedFS = value;
NotifyPropertyChanged();
}
}
XAML: XAML:
d:DataContext="{d:DesignInstance Type=anzeigen:FSViewModel}"
FSViewModel
contains the FSList
, and FavoritCommand
which fills its duty FSViewModel
包含FSList
和FavoritCommand
履行其职责
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding FSList}"
SelectedItem="{Binding SelectedFS}">
<ListBox.ItemTemplate>
<DataTemplate>
<materialDesign:Card >
<Grid>
<ToggleButton Style="{StaticResource MaterialDesignFlatPrimaryToggleButton}"
IsChecked="False"
Command="{Binding DataContext.FavoritCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
CommandParameter="{Binding}" />
</Grid>
</materialDesign:Card>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I didn't quite understand your question, so this is more of a recommendations.我不太明白你的问题,所以这更像是一个建议。
But perhaps one of them will be the solution to your problem.但也许其中之一将解决您的问题。
You have incorrectly implemented the FSList
property.您错误地实现了
FSList
属性。 Each time this property is accessed, a new query will be made to the BD and a new collection will be returned.每次访问该属性时,都会对 BD 进行一次新的查询,并返回一个新的集合。 Including, all copies of FilmeSerien in this collection will also be new.
包括,此系列中的所有 FilmeSerien 副本也将是新的。 But according to the logic of the Presentation, you will consider them the same.
但是根据 Presentation 的逻辑,你会认为它们是一样的。 Such an implementation is likely to lead to some kind of bugs.
这样的实现很可能会导致某种错误。
It is preferable to use the DB context (variable _db
) one-time in the using block.最好在 using 块中一次性使用 DB 上下文(变量
_db
)。 We made a request, get data from it and then destroyed it.我们提出了一个请求,从中获取数据,然后将其销毁。 Otherwise, with each request, you need to take into account the results of the previous one, and resources used to implement the request will not be released between requests.
否则,对于每一个请求,都需要考虑上一个请求的结果,用于执行请求的资源不会在请求之间释放。
Implementation example:实现示例:
public IEnumerable<FilmeSerien> FSList {get;}
// ViewModel constructor
public FSViewModel()
{
// I don't know the name of your DB context implementation type,
// so the name is conditional
using(var db = new AppDbContext())
FSList = db.FilmeSeriens
.Include(x => x.Bewertungens)
.AsNoTracking()
.ToList();
}
An example of implementation in the simplest case:最简单情况下的实现示例:
<UserControl .....
DataContext="{DynamicResource viewModel}">
<UserControl.Resources>
<anzeigen:FSViewModel x:Key="viewModel"/>
<UserControl.Resources>
<ToggleButton Command="{Binding FavoritCommand,
Source={StaticResource viewModel}}" .../>
d: DesignInstance
, to define the design mode in the ViewModel and fill its instance in this with demo data.d: DesignInstance
。 An example of such a ViewModel implementation:这种 ViewModel 实现的示例:
private static bool IsInDesignMode { get; }
= DesignerProperties.GetIsInDesignMode(new DependencyObject());
public FSViewModel()
{
if (IsInDesignMode)
{
FSList = new List<FilmeSerien>()
{
// Several FilmeSerien instances are created here for the demo mode.
new FilmeSerien(...){....},
new FilmeSerien(...){....},
new FilmeSerien(...){....},
};
}
else
{
// Here is the code that runs when the Application is executed
using(var db = new AppDbContext())
FSList = db.FilmeSeriens
.Include(x => x.Bewertungens)
.AsNoTracking()
.ToList();
}
}
Hopefully some of this will help you.希望其中一些对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.