0

I have two combobox which are currently binded to the user model. The first combobox supposed to show the current userRole value before clicking on the combobox. The other combobox supposed to show the userStatus either 1 or 0. Now the second combobox is not displaying any value. However the first combobox is displaying the value once its clicked only.

Here is the xaml code:

<StackPanel Orientation="Horizontal">
<TextBlock FontSize="12" Text="User Role: " VerticalAlignment="Center" />
<ComboBox x:Name="cbUserRole" FlowDirection="LeftToRight" FontSize="16" Foreground="MidnightBlue" HorizontalAlignment="Stretch" VerticalAlignment="Center" Loaded="cbUserRole_Loaded" SelectedItem="{Binding UserRole, Mode=TwoWay" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="12" Text="User Status: " VerticalAlignment="Center" />
<ComboBox x:Name="cbUserStatus" FlowDirection="LeftToRight" FontSize="16" Foreground="MidnightBlue" HorizontalAlignment="Stretch" VerticalAlignment="Center" SelectedIndex="{Binding UserStatus, Converter={StaticResource boolToIndexConverter}}" />
</StackPanel>

Here is my converter code:

 public class BoolToIndexConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((bool)value == true) ? 0 : 1;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((int)value == 0) ? true : false;
    }
}

The code above is refered from this link:

Here is my User model code:

private string userrole;


    public string UserRole
    {
        get { return userrole; }
        set
        {
            userrole = value;
            OnPropertyChanged("UserRole");
        }
    }

private bool userstatus;


    public bool UserStatus
    {
        get { return userstatus; }
        set
        {
            userstatus = value;
            OnPropertyChanged("UserStatus");
        }
    }

How can i fix this problems? I did search and tried from different blogs but its not working for me.

1 Answers1

0

The problem is that your combobox's DataContext is User, but you need to set ItemsSource from DataGrid's datacontext. In order to do that you need to use the following syntax:

 <ComboBox 
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}}, Path=DataContext.Users}" 
                                          x:Name="cbUserRole" FlowDirection="LeftToRight" FontSize="16" Foreground="MidnightBlue" HorizontalAlignment="Stretch" VerticalAlignment="Center" 
                                          SelectedItem="{Binding UserRole,Mode=TwoWay}"></ComboBox>

And Remove Loaded handler.

You can also use <DataGrid DataContext="{StaticResource uvm}" instead of codebehind code:

var userList = new UserViewModel().Users;
userDataGrid.ItemsSource = userList;

You don't have ItemsSource for your Status Combobox. I think you should add collection of statuses to UserViewModel and bind it like in the example above.

Access Denied
  • 8,723
  • 4
  • 42
  • 72
  • `RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}},` the following code is populating the whole user roles, I dont want that for my case. In my database there is only 4 user roles. – anonymous_apple Sep 06 '18 at 07:05
  • @anonymous_apple Make another property instead of getUserRoles() create UserRoles property in your view model. – Access Denied Sep 06 '18 at 07:19
  • and change xaml in my sample to Path=DataContext.UserRoles – Access Denied Sep 06 '18 at 07:20
  • **I think you should add collection of statuses to UserViewModel and bind it like in the example above.** Based on the bold words, I am not getting the idea....The status is either Enable or Disable only. How can I achieve that? If u see my code I created a IValueConverter but its not working... – anonymous_apple Sep 06 '18 at 07:25
  • You have combobox and no items in it, maybe you should use checkbox? – Access Denied Sep 06 '18 at 07:26