当我通过视图模型更改 currentUser 时,如何更新我的 XAML?
How can I update my XAML when I change currentUser via viewmodel?
我正在创建我的第一个 'bigger' WPF 应用程序,我想到了类似 tinder 的东西。
我已经创建了一张如下所示的卡片:
Result
它显示了我数据库中的第一个用户。我的目标是当我点击右(下一个)或左(上一个)按钮时,显示我数据库中的下一个或上一个用户。
这是我的 class Gebruiker 模型(对不起荷兰名字..):
namespace project_amber_akkermans.Model
{
class Gebruiker : BaseModel
{
private int id;
private string naam;
private string adres;
private string woonplaats;
private string email;
private string gsm;
private string functie;
private string bestemming;
public int ID { get; set; }
public string Naam { get; set; }
public string Adres { get; set; }
public string Woonplaats { get; set; }
public string Email { get; set; }
public string Gsm { get; set; }
public string Bestemming { get; set; }
public string Functie { get; set; }
}
}
我的视图模型,我可以看到我的下一个和上一个命令确实更新了 id,并且执行了 sql,但我不知道我的 currentGebruiker 是否没有更新,或者我的 XAML 不更新:
class ZoekMatchViewModel : BaseViewModel
{
public ZoekMatchViewModel()
{
LeesGebruiker(1);
KoppelenCommands();
}
private int aantal_gebruikers;
public int Aantal_gebruikers
{
get
{
return aantal_gebruikers;
}
set
{
aantal_gebruikers = value;
NotifyPropertyChanged();
}
}
private Gebruiker currentGebruiker;
public Gebruiker CurrentGebruiker
{
get
{
return currentGebruiker;
}
set
{
currentGebruiker = value;
NotifyPropertyChanged();
}
}
private void KoppelenCommands()
{
NextCommand = new BaseCommand(VolgendeGebruiker);
PrevCommand = new BaseCommand(VorigeGebruiker);
}
public ICommand NextCommand { get; set; }
public ICommand PrevCommand { get; set; }
private void LeesGebruikers()
{
//instantiëren dataservice
ZoekMatchDataService zoekMatchDS =
new ZoekMatchDataService();
aantal_gebruikers = zoekMatchDS.GetGebruikers();
}
private void LeesGebruiker(int id)
{
//instantiëren dataservice
ZoekMatchDataService zoekMatchDS =
new ZoekMatchDataService();
currentGebruiker = zoekMatchDS.GetGebruiker(id);
}
public void VolgendeGebruiker()
{
if (CurrentGebruiker != null)
{
if (currentGebruiker.ID+1 != aantal_gebruikers) //if id +1 != number of users in the database, if last user in database, jump back to first
{
int id = (currentGebruiker.ID) + 1;
LeesGebruiker(id);
}
else
{
int id = 1;
LeesGebruiker(id);
}
}
}
public void VorigeGebruiker()
{
if (CurrentGebruiker != null)
{
if (currentGebruiker.ID-1 != 0) //if id -1 != 0 (lowest id = 1, so if 0, jump to last user)
{
int id = (currentGebruiker.ID) - 1;
LeesGebruiker(id);
}
else
{
int id = aantal_gebruikers;
LeesGebruiker(id);
}
}
}
}
Dataservice class 从我的数据库中获取数据(我使用的是 Dapper):
class ZoekMatchDataService
{
// Ophalen ConnectionString uit App.config
private static string connectionString =
ConfigurationManager.ConnectionStrings["local"].ConnectionString;
// Stap 1 Dapper
// Aanmaken van een object uit de IDbConnection class en
// instantiëren van een SqlConnection.
// Dit betekent dat de connectie met de database automatisch geopend wordt.
private static IDbConnection db = new SqlConnection(connectionString);
public int GetGebruikers()
{
// Stap 2 Dapper
// Uitschrijven SQL statement & bewaren in een string.
string sql = "Select * from Gebruiker";
// Stap 3 Dapper
// Uitvoeren SQL statement op db instance
// Type casten van het generieke return type naar een collectie van contactpersonen
return (int)db.Query<Gebruiker>(sql).Count();
}
public Gebruiker GetGebruiker(int id)
{
// Stap 2 Dapper
// Uitschrijven SQL statement & bewaren in een string.
string sql = "Select * from Gebruiker where ID = " + id;
// Stap 3 Dapper
// Uitvoeren SQL statement op db instance
return (Gebruiker)db.Query<Gebruiker>(sql).FirstOrDefault();
}
public void InsertMatch(Gebruiker gebruiker)
{
// SQL statement insert
string date = DateTime.UtcNow.ToString("dd-MM-YYYY");
string sql = "Insert into Match (Gebruiker1Id, Gebruiker2Id, Datum, Geaccepteerd) values (1," + gebruiker.ID + ","+ date +", false)";
// Uitvoeren SQL statement en doorgeven parametercollectie
db.Execute(sql, new
{
gebruiker.Naam,
gebruiker.Email
});
}
}
我的 XAML 中最重要的部分:
<Page x:Class="project_amber_akkermans.View.MakeMatch"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:project_amber_akkermans.View"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:smtx="clr-namespace:ShowMeTheXAML;assembly=ShowMeTheXAML"
xmlns:viewmodel="clr-namespace:project_amber_akkermans.ViewModel"
mc:Ignorable="d"
Title="MakeMatch" Height="450" Width="800">
<Page.Resources>
<viewmodel:ZoekMatchViewModel x:Key="ZoekMatchViewModel"/>
</Page.Resources>
<DockPanel LastChildFill="True" HorizontalAlignment="Center" VerticalAlignment="Center" DataContext="{DynamicResource ZoekMatchViewModel}">
<Button Command="{Binding PrevCommand}" Background="Transparent" BorderThickness="0" Height="50">
<StackPanel Orientation="Horizontal" DockPanel.Dock="Left" VerticalAlignment="Center" Margin="0 -10 35 0">
<materialDesign:PackIcon Kind="ChevronLeft" Foreground="DarkGoldenrod" Background="Transparent" Margin="0 0 -50 0" Width="65" Height="65" />
<materialDesign:PackIcon Kind="ChevronLeft" Foreground="LightSlateGray" Margin="0 0 -50 0" Width="65" Height="65"/>
<materialDesign:PackIcon Kind="ChevronLeft" Foreground="WhiteSmoke" Margin="0 0 -50 0" Width="65" Height="65"/>
</StackPanel>
</Button>
<smtx:XamlDisplay Key="cards_1" Margin="4 4 0 0">
<materialDesign:Flipper Style="{StaticResource MaterialDesignCardFlipper}">
<materialDesign:Flipper.FrontContent>
<Grid Height="350" Width="200">
<Grid.RowDefinitions>
<RowDefinition Height="250" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<materialDesign:ColorZone Mode="PrimaryMid" VerticalAlignment="Stretch">
<materialDesign:PackIcon Kind="AccountCircle" Height="128" Width="128"
VerticalAlignment="Center" HorizontalAlignment="Center" />
</materialDesign:ColorZone>
<StackPanel Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="{Binding CurrentGebruiker.Naam , Mode=TwoWay}"></TextBlock>
<Button Style="{StaticResource MaterialDesignFlatButton}" Foreground="DarkGoldenrod"
Command="{x:Static materialDesign:Flipper.FlipCommand}"
Margin="0 4 0 0"
>SHOW DETAILS</Button>
</StackPanel>
</Grid>
</materialDesign:Flipper.FrontContent>
我希望我的问题足够清楚,并希望任何人都可以帮助我解决它
你已经满了属性
private Gebruiker currentGebruiker;
public Gebruiker CurrentGebruiker
{
get
{
return currentGebruiker;
}
set
{
currentGebruiker = value;
NotifyPropertyChanged();
}
}
并使用后场更改其值:
private void LeesGebruiker(int id)
{
...
currentGebruiker = zoekMatchDS.GetGebruiker(id);
}
如果您这样做,则不会调用 属性 setter,不会发出通知,也不会更新绑定。
使用属性:
CurrentGebruiker = zoekMatchDS.GetGebruiker(id);
我正在创建我的第一个 'bigger' WPF 应用程序,我想到了类似 tinder 的东西。 我已经创建了一张如下所示的卡片: Result
它显示了我数据库中的第一个用户。我的目标是当我点击右(下一个)或左(上一个)按钮时,显示我数据库中的下一个或上一个用户。
这是我的 class Gebruiker 模型(对不起荷兰名字..):
namespace project_amber_akkermans.Model
{
class Gebruiker : BaseModel
{
private int id;
private string naam;
private string adres;
private string woonplaats;
private string email;
private string gsm;
private string functie;
private string bestemming;
public int ID { get; set; }
public string Naam { get; set; }
public string Adres { get; set; }
public string Woonplaats { get; set; }
public string Email { get; set; }
public string Gsm { get; set; }
public string Bestemming { get; set; }
public string Functie { get; set; }
}
}
我的视图模型,我可以看到我的下一个和上一个命令确实更新了 id,并且执行了 sql,但我不知道我的 currentGebruiker 是否没有更新,或者我的 XAML 不更新:
class ZoekMatchViewModel : BaseViewModel
{
public ZoekMatchViewModel()
{
LeesGebruiker(1);
KoppelenCommands();
}
private int aantal_gebruikers;
public int Aantal_gebruikers
{
get
{
return aantal_gebruikers;
}
set
{
aantal_gebruikers = value;
NotifyPropertyChanged();
}
}
private Gebruiker currentGebruiker;
public Gebruiker CurrentGebruiker
{
get
{
return currentGebruiker;
}
set
{
currentGebruiker = value;
NotifyPropertyChanged();
}
}
private void KoppelenCommands()
{
NextCommand = new BaseCommand(VolgendeGebruiker);
PrevCommand = new BaseCommand(VorigeGebruiker);
}
public ICommand NextCommand { get; set; }
public ICommand PrevCommand { get; set; }
private void LeesGebruikers()
{
//instantiëren dataservice
ZoekMatchDataService zoekMatchDS =
new ZoekMatchDataService();
aantal_gebruikers = zoekMatchDS.GetGebruikers();
}
private void LeesGebruiker(int id)
{
//instantiëren dataservice
ZoekMatchDataService zoekMatchDS =
new ZoekMatchDataService();
currentGebruiker = zoekMatchDS.GetGebruiker(id);
}
public void VolgendeGebruiker()
{
if (CurrentGebruiker != null)
{
if (currentGebruiker.ID+1 != aantal_gebruikers) //if id +1 != number of users in the database, if last user in database, jump back to first
{
int id = (currentGebruiker.ID) + 1;
LeesGebruiker(id);
}
else
{
int id = 1;
LeesGebruiker(id);
}
}
}
public void VorigeGebruiker()
{
if (CurrentGebruiker != null)
{
if (currentGebruiker.ID-1 != 0) //if id -1 != 0 (lowest id = 1, so if 0, jump to last user)
{
int id = (currentGebruiker.ID) - 1;
LeesGebruiker(id);
}
else
{
int id = aantal_gebruikers;
LeesGebruiker(id);
}
}
}
}
Dataservice class 从我的数据库中获取数据(我使用的是 Dapper):
class ZoekMatchDataService
{
// Ophalen ConnectionString uit App.config
private static string connectionString =
ConfigurationManager.ConnectionStrings["local"].ConnectionString;
// Stap 1 Dapper
// Aanmaken van een object uit de IDbConnection class en
// instantiëren van een SqlConnection.
// Dit betekent dat de connectie met de database automatisch geopend wordt.
private static IDbConnection db = new SqlConnection(connectionString);
public int GetGebruikers()
{
// Stap 2 Dapper
// Uitschrijven SQL statement & bewaren in een string.
string sql = "Select * from Gebruiker";
// Stap 3 Dapper
// Uitvoeren SQL statement op db instance
// Type casten van het generieke return type naar een collectie van contactpersonen
return (int)db.Query<Gebruiker>(sql).Count();
}
public Gebruiker GetGebruiker(int id)
{
// Stap 2 Dapper
// Uitschrijven SQL statement & bewaren in een string.
string sql = "Select * from Gebruiker where ID = " + id;
// Stap 3 Dapper
// Uitvoeren SQL statement op db instance
return (Gebruiker)db.Query<Gebruiker>(sql).FirstOrDefault();
}
public void InsertMatch(Gebruiker gebruiker)
{
// SQL statement insert
string date = DateTime.UtcNow.ToString("dd-MM-YYYY");
string sql = "Insert into Match (Gebruiker1Id, Gebruiker2Id, Datum, Geaccepteerd) values (1," + gebruiker.ID + ","+ date +", false)";
// Uitvoeren SQL statement en doorgeven parametercollectie
db.Execute(sql, new
{
gebruiker.Naam,
gebruiker.Email
});
}
}
我的 XAML 中最重要的部分:
<Page x:Class="project_amber_akkermans.View.MakeMatch"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:project_amber_akkermans.View"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:smtx="clr-namespace:ShowMeTheXAML;assembly=ShowMeTheXAML"
xmlns:viewmodel="clr-namespace:project_amber_akkermans.ViewModel"
mc:Ignorable="d"
Title="MakeMatch" Height="450" Width="800">
<Page.Resources>
<viewmodel:ZoekMatchViewModel x:Key="ZoekMatchViewModel"/>
</Page.Resources>
<DockPanel LastChildFill="True" HorizontalAlignment="Center" VerticalAlignment="Center" DataContext="{DynamicResource ZoekMatchViewModel}">
<Button Command="{Binding PrevCommand}" Background="Transparent" BorderThickness="0" Height="50">
<StackPanel Orientation="Horizontal" DockPanel.Dock="Left" VerticalAlignment="Center" Margin="0 -10 35 0">
<materialDesign:PackIcon Kind="ChevronLeft" Foreground="DarkGoldenrod" Background="Transparent" Margin="0 0 -50 0" Width="65" Height="65" />
<materialDesign:PackIcon Kind="ChevronLeft" Foreground="LightSlateGray" Margin="0 0 -50 0" Width="65" Height="65"/>
<materialDesign:PackIcon Kind="ChevronLeft" Foreground="WhiteSmoke" Margin="0 0 -50 0" Width="65" Height="65"/>
</StackPanel>
</Button>
<smtx:XamlDisplay Key="cards_1" Margin="4 4 0 0">
<materialDesign:Flipper Style="{StaticResource MaterialDesignCardFlipper}">
<materialDesign:Flipper.FrontContent>
<Grid Height="350" Width="200">
<Grid.RowDefinitions>
<RowDefinition Height="250" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<materialDesign:ColorZone Mode="PrimaryMid" VerticalAlignment="Stretch">
<materialDesign:PackIcon Kind="AccountCircle" Height="128" Width="128"
VerticalAlignment="Center" HorizontalAlignment="Center" />
</materialDesign:ColorZone>
<StackPanel Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="{Binding CurrentGebruiker.Naam , Mode=TwoWay}"></TextBlock>
<Button Style="{StaticResource MaterialDesignFlatButton}" Foreground="DarkGoldenrod"
Command="{x:Static materialDesign:Flipper.FlipCommand}"
Margin="0 4 0 0"
>SHOW DETAILS</Button>
</StackPanel>
</Grid>
</materialDesign:Flipper.FrontContent>
我希望我的问题足够清楚,并希望任何人都可以帮助我解决它
你已经满了属性
private Gebruiker currentGebruiker;
public Gebruiker CurrentGebruiker
{
get
{
return currentGebruiker;
}
set
{
currentGebruiker = value;
NotifyPropertyChanged();
}
}
并使用后场更改其值:
private void LeesGebruiker(int id)
{
...
currentGebruiker = zoekMatchDS.GetGebruiker(id);
}
如果您这样做,则不会调用 属性 setter,不会发出通知,也不会更新绑定。
使用属性:
CurrentGebruiker = zoekMatchDS.GetGebruiker(id);