[英]c# wpf Exception Unhandled
I have a problem with wpf and changing two wpf pages while pressing button. 我在使用wpf并在按下按钮时更改两个wpf页面时遇到问题。 when i press the button that should go to the other page, visual-studio throws this error: 当我按下应该转到另一页的按钮时,visual-studio会引发此错误:
System.InvalidOperationException
: 'A TwoWay or OneWayToSource binding cannot work on the read-only property 'Boozes' of type 'DatabaseBoozeWpf.ViewModels.MainWindowVM'.' System.InvalidOperationException
:'TwoWay或OneWayToSource绑定不能在类型为'DatabaseBoozeWpf.ViewModels.MainWindowVM'的只读属性'Boozes'上使用。
my code for button: 我的按钮代码:
private void exitAddItemWindow(object sender, RoutedEventArgs e)
{
MainWindow sK = new MainWindow();
sK.Show();
this.Close();
}
If i try same project in other computer, it works. 如果我在其他计算机上尝试相同的项目,则可以。 What should I do? 我该怎么办?
My Xaml code: exit button will activate exitAddItemWindow 我的Xaml代码:退出按钮将激活exitAddItemWindow
<Window x:Class="DatabaseBoozeWpf.AddItemsForm.AddItemWindow"
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:DatabaseBoozeWpf.AddItemsForm"
mc:Ignorable="d"
Title="Add items" Height="300" Width="400">
<Grid Background="#e6e6e6">
<Button Content="Exit" HorizontalAlignment="Left" Margin="184,179,0,0" Background="#FF3496B4"
VerticalAlignment="Top" Click="exitAddItemWindow" Width="60" Height="19">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5" />
</Style>
</Button.Resources>
</Button>
<Button Content="Login" HorizontalAlignment="Left" Margin="274,179,0,0" Background="#FF3496B4"
VerticalAlignment="Top" Click="loginNewWindow" Width="75" Height="19">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"/>
</Style>
</Button.Resources>
</Button>
<TextBox Name="TextBox1" HorizontalAlignment="Left" Height="23" Margin="229,55,0,0" Background="#FF79DCFA"
BorderBrush="#FF0040FF" TextWrapping="Wrap" VerticalAlignment="Top" Width="120">
</TextBox>
<PasswordBox Name="TextBox2" HorizontalAlignment="Left" Height="23" Background="#FF79DCFA" BorderBrush="#FF0040FF"
Margin="229,101,0,0" VerticalAlignment="Top" Width="120">
<PasswordBox.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="25"/>
</Style>
</PasswordBox.Resources>
</PasswordBox>
<Label Content="Username:" HorizontalAlignment="Left" Margin="166,52,0,0" VerticalAlignment="Top"/>
<Label Content="Password:" HorizontalAlignment="Left" Margin="166,98,0,0" VerticalAlignment="Top"/>
</Grid>
Code For MainWindow.Xaml
: MainWindow.Xaml
代码:
<Window x:Name="Bar" x:Class="DatabaseBoozeWpf.MainWindow"
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:DatabaseBoozeWpf"
mc:Ignorable="d"
Title="Virtual Bar" Height="450" Width="625">
<Grid Background="#e6e6e6">
<ListBox Name="BoozeList" Margin="10,124,0,10" HorizontalAlignment="Left"
Width="233" Background="#FF79DCFA" BorderBrush="#FF0040FF">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding UnitPrice, StringFormat={}{0} Euros }" />
<TextBlock Text="{Binding Quantity, StringFormat={}{0} ml }" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Resources>
<CollectionViewSource x:Key="BoozesCollection" Source="{Binding Boozes}"/>
<CollectionViewSource x:Key="JuicesCollection" Source="{Binding Juices}"/>
<CollectionViewSource x:Key="SnacksCollection" Source="{Binding Snacks}"/>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="15"/>
</Style>
</ListBox.Resources>
<ListBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource BoozesCollection}}"/>
<CollectionContainer Collection="{Binding Source={StaticResource JuicesCollection}}"/>
<CollectionContainer Collection="{Binding Source={StaticResource SnacksCollection}}"/>
</CompositeCollection>
</ListBox.ItemsSource>
</ListBox>
<TextBox Height="27" HorizontalAlignment="Left" Margin="10,92,0,0"
Name="SearchBox" VerticalAlignment="Top" Width="130"
Background="#FF51C1FA"
Text="{Binding Boozes, UpdateSourceTrigger=PropertyChanged}" >
<TextBox.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="10"/>
</Style>
</TextBox.Resources>
</TextBox>
<Label Content="Search" HorizontalAlignment="Left" Margin="10,64,0,0" VerticalAlignment="Top"/>
<Button Content="Add to cart" HorizontalAlignment="Left" Margin="274,92,0,0" VerticalAlignment="Top" Width="106" Click="Button_Click" Background="#FF3496B4">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="10"/>
</Style>
</Button.Resources>
</Button >
<PasswordBox Name="Pass" HorizontalAlignment="Left" Margin="411,0,0,114" Width="82" Background="#FF51C1FA" Height="18" VerticalAlignment="Bottom">
<PasswordBox.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="10"/>
</Style>
</PasswordBox.Resources>
</PasswordBox>
<Button Content="Pay" HorizontalAlignment="Left" Margin="498,0,0,113" Width="79" Background="#FF3496B4"
Height="19" VerticalAlignment="Bottom" Click="Button_Click_2">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"/>
</Style>
</Button.Resources>
</Button>
<ListBox Name="OrderList" HorizontalAlignment="Left" Margin="411,92,0,193" Width="166" Background="#FF79DCFA" BorderBrush="#FF0040FF" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="15"/>
</Style>
</ListBox.Resources>
</ListBox>
<Label Content="Cart" HorizontalAlignment="Left" Margin="416,64,0,0" VerticalAlignment="Top" Width="77"/>
<Label Content="Enter your pincode:" HorizontalAlignment="Left" Margin="411,0,0,132" Height="25" VerticalAlignment="Bottom"/>
<Button Content="Remove from cart" HorizontalAlignment="Left" Margin="274,124,0,0" VerticalAlignment="Top"
Width="106" Height="22" Click="Button_Click_1" Background="#FF3496B4">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="10"/>
</Style>
</Button.Resources>
</Button >
<Button Content="Add items" HorizontalAlignment="Left" Margin="502,0,0,26" Click="btnAddItems"
Width="75" Background="#FF3496B4" Height="20" VerticalAlignment="Bottom"/>
<Button Content="Search" HorizontalAlignment="Left" Margin="145,92,0,0" VerticalAlignment="Top"
Width="77" Click="Button_Click_3" Background="#FF3496B4" Height="24">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="10"/>
</Style>
</Button.Resources>
</Button >
</Grid>
MainWindowVM.cs
code: MainWindowVM.cs
代码:
public class MainWindowVM : BaseVM
{
private List<Booze> _boozes;
public List<Booze> Boozes
{
get { return _boozes; }
private set
{
_boozes = value;
base.NotifyPropertyChanged("Boozes");
}
}
private IBoozeService _boozeService;
public MainWindowVM()
{
_boozes = new List<Booze>();
_boozeService = new BoozeService(new BoozeDbContext());
}
public void LoadData()
{
Boozes = _boozeService.GetAllBoozes();
}
}
Set the Mode
of the binding to the Booze
property to OneWay
in your XAML: 在XAML中将绑定到Booze
属性的Mode
设置为OneWay
:
<TextBox Text="{Binding Booze, Mode=OneWay}" />
...or add a public setter to the Booze
source property: ...或向Booze
源属性添加公共设置器:
public string Booze { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.