簡體   English   中英

c#wpf未處理異常

[英]c# wpf Exception Unhandled

我在使用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();

    }

}

在XAML中將綁定到Booze屬性的Mode設置為OneWay

<TextBox Text="{Binding Booze, Mode=OneWay}" />

...或向Booze源屬性添加公共設置器:

public string Booze { get; set; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM