我遇到了wpf的问题,并在按下按钮时更改了两个wpf页面。当我按下应该转到另一页的按钮时,visual-studio会抛出此错误:
System.InvalidOperationException
:'TwoWay或OneWayToSource绑定无法在'DatabaseBoozeWpf.ViewModels.MainWindowVM'类型的只读属性'Boozes'上运行。'
我的按钮代码:
private void exitAddItemWindow(object sender, RoutedEventArgs e)
{
MainWindow sK = new MainWindow();
sK.Show();
this.Close();
}
如果我在其他计算机上尝试相同的项目,它的工作原理。我该怎么办?
我的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>
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
代码:
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();
}
}
答案 0 :(得分:1)
在XAML中将绑定的Mode
设置为Booze
属性为OneWay
:
<TextBox Text="{Binding Booze, Mode=OneWay}" />
...或将公共设置器添加到Booze
源属性:
public string Booze { get; set; }