简体   繁体   中英

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 . I've got a List of filme_serien . And I am trying to bind the isfavorit form the bewertungen table through the filme_serien List.

ER:

在此处输入图像描述

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 :

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:

d:DataContext="{d:DesignInstance Type=anzeigen:FSViewModel}"

FSViewModel contains the FSList , and FavoritCommand which fills its duty

<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. Each time this property is accessed, a new query will be made to the BD and a new collection will be returned. Including, all copies of FilmeSerien in this collection will also be new. But according to the logic of the Presentation, you will consider them the same. 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. 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. 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.

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.

An example of such a ViewModel implementation:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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