0

I have a problem with wpf and changing two wpf pages while pressing button. when i press the button that should go to the other page, visual-studio throws this error:

System.InvalidOperationException: 'A TwoWay or OneWayToSource binding cannot work on the read-only property 'Boozes' of type 'DatabaseBoozeWpf.ViewModels.MainWindowVM'.'

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

<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:

<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:

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();

    }

}
7
  • Show us your XAML. Commented Jan 25, 2018 at 10:56
  • Added XAML code Commented Jan 25, 2018 at 11:21
  • Could you show us XAML of MainWindow? Commented Jan 25, 2018 at 11:29
  • Added MainWindow code Commented Jan 25, 2018 at 11:46
  • 1
    In your set property Boozes remove private. Commented Jan 25, 2018 at 11:55

1 Answer 1

1

Set the Mode of the binding to the Booze property to OneWay in your XAML:

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

...or add a public setter to the Booze source property:

public string Booze { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.