简体   繁体   English

绑定:如何绑定另一个 class 的属性

[英]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 : FSListSelectedFSFSViewModel中:

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包含FSListFavoritCommand履行其职责

<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.但也许其中之一将解决您的问题。

  1. 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.这样的实现很可能会导致某种错误。

  2. 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();
}
  1. To simplify access to the ViewModel, it is very convenient to set its instance in resources.为了简化对 ViewModel 的访问,在资源中设置它的实例非常方便。 You can specify the instance itself, you can use some auxiliary container class in the properties of which there will be the necessary data, including the ViewModel.可以指定实例本身,也可以使用一些辅助容器class,其中的属性中会有必要的数据,包括ViewModel。

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}}" .../>
  1. For convenient detection of errors during design, it is better, instead of d: DesignInstance , to define the design mode in the ViewModel and fill its instance in this with demo data.为了方便在设计过程中发现错误,最好在 ViewModel 中定义设计模式,并在其中填充演示数据,而不是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM