0

Am new to WPF. I am creating a WPF 4.5 app with MVVM using Prism 4 and MahApp Metro. In the page I have a combobox where a list of items will be shown and on SelectedItem I would like to show the details for the item in a form below.

Now here is the code for ViewEditItemsView.xaml

<UserControl x:Class="WPFSample.Module.Item.View.ViewEditItemsView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
         xmlns:local="clr-namespace:WPFSample.Module.Item.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="500" d:DesignWidth="350">
<UserControl.DataContext>
    <local:ViewEditItemsViewModel/>
</UserControl.DataContext>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="75"/>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition Width="40"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBlock x:Name="tbkSearch" Grid.Column="0" Grid.Row="0" Text="Search Item" VerticalAlignment="Top" Margin="0,14,0,0" HorizontalAlignment="Right" Width="70" />
    <ComboBox Width="200" Grid.Column="1" Grid.Row="0" VerticalAlignment="Top"
                  Margin="0, 10, 0, 0"
                  Style="{DynamicResource VirtualisedMetroComboBox}"
                  Controls:TextBoxHelper.Watermark="Autocompletion"
                  DisplayMemberPath="ItemName"
                  IsEditable="True"
                  ItemsSource="{Binding Items}"
                  MaxDropDownHeight="125"
                  Text="{Binding Path=ItemName}"
               SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"/>
    <Button x:Name="PART_ForwardButton" Grid.Column="2" Grid.Row="0"
                DockPanel.Dock="Right"
                Style="{DynamicResource MetroCircleButtonStyle}"
                Height="40"
                Width="40"
                FontFamily="Segoe UI Symbol"
                FontSize="16"
                VerticalAlignment="Top"
                HorizontalAlignment="Left"
                IsEnabled="True">
        <Rectangle Width="20"
                       Height="15">
            <Rectangle.Fill>
                <VisualBrush Stretch="Fill">
                    <VisualBrush.Visual>
                        <Canvas Width="48"
                                    Height="48"
                                    Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0"
                                    UseLayoutRounding="False">
                            <Path Width="25" ToolTip="Add File"
                                      Height="18"
                                      Canvas.Left="12"
                                      Canvas.Top="15"
                                      Stretch="Fill"
                                      Fill="{DynamicResource BlackBrush}"
                                      Data="F1 M 35,19L 41,19L 41,35L 57,35L 57,41L 41,41L 41,57L 35,57L 35,41L 19,41L 19,35L 35,35L 35,19 Z " />
                        </Canvas>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Button>
    <Grid x:Name="grdFileDetails" Grid.ColumnSpan="2" Grid.Column="0" Grid.Row="1" DataContext="{Binding SelectedItem}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBlock x:Name="tbkItemName" Grid.Column="0" Text="Item Name" VerticalAlignment="Top" Margin="0,26,197,0" HorizontalAlignment="Right" Width="70" Grid.ColumnSpan="2" />
        <TextBox x:Name="txtItemName" Text="{Binding Path=ItemName,Mode=OneWay}" Grid.Column="1" IsEnabled="False"/>
    </Grid>
</Grid>

Code for ViewEditItemsViewModel.cs

public class ViewEditItemsViewModel : ViewModelBase
{
    private ObservableCollection<ItemViewModel> items;
    private ItemViewModel selectedItem;
    public ViewEditItemsViewModel()
    {
        var itemModel = new ItemModel();
        this.items = itemModel.GetAllItems();
    }
    public ObservableCollection<ItemViewModel> Items
    {
        get
        { return items; }
        set
        {
            items = value;
            OnPropertyChanging("Items");
        }
    }

    public ItemViewModel SelectedItem
    {
        get { return selectedItem; }
        set
        {
            selectedItem = value;
            OnPropertyChanging("SelectedItem");
        }
    }
}

Code for ItemViewModel.cs

public class ItemViewModel:ViewModelBase
{
    private int id;
    private string itemName;

    public int Id
    {
        get { return id;}
        set
        {
            id = value;
            OnPropertyChanging("Id");
        }
    }
    public string ItemName
    {
        get { return itemName; }
        set
        {
            itemName = value;
            OnPropertyChanging("ItemName");
        }
    }
}

ViewModelBase.cs is basically implementing INotifyPropertyChanged and INotifyPropertyChanging to be sure. And ItemModel.cs is fetching the data from the database.

Where am I doing wrong? The txtItemName Textbox is not getting populated.

Thomas Freudenberg
  • 5,048
  • 1
  • 35
  • 44
pasaban
  • 91
  • 1
  • 11
  • Are there any typo? Because I don't see `SelectedFile` in any ViewModel. And your `Grid` - which contains TextBox - has `DataContext` `SelectedFile`, so your `TextBox` binding is try to find `SelectedFile.SelectedFile.ItemName`. – sac1 May 28 '15 at 17:11
  • @sac1 - Thanks for pointing out the typo corrected that. Still no luck. Please help... – pasaban May 29 '15 at 08:02
  • Debug it in Visual Studio and check Output. Maybe it will contain binding error. – sac1 May 29 '15 at 10:37
  • One more thing: Only `PropertyChangedEvent` is relevant. So replace your Changing event in properties setters to Changed event. – sac1 May 29 '15 at 10:45
  • Thanks a million sac1!!!! The issue was with changing.. the moment I changed it to PropertyChangedEvent it worked. Silly me!! Thanks again! – pasaban May 29 '15 at 11:44

0 Answers0