开发者

Problem with Silverlight ComboBoxItem binding

I just can't make the following situation work: I have a class, with the following implementation:

public class SelectionItem<T> : ViewModelBase where T : Entity
{
    private bool? _isSelected;
    public bool? IsSelected
    {
        get { return _isSelected; }
        set
        {
            _isSelected = value;
            RaisePropertyChanged("IsSelected");
        }
    }

    public T Item { get; set; }
}

And I have the following property on my ViewModel:

    private IEnumerable<SelectionItem<DB_Aux_Pessoas>> _vendedores;
    public IEnumerable<SelectionItem<DB_Aux_Pessoas>> Vendedores
    {
        get
        {
            return _vendedores;
        }
        set
        {
            _vendedores = value;
            RaisePropertyChanged("Vendedores");
        }
    }

Then, in my View, I have the ComboBox:

                    <ComboBox Margin="3,0,0,0"
                              Height="23"
                              Width="200"
                              ItemsSource="{Binding Vendedores, Mode=TwoWay}"
                              Grid.Column="1"
                              Grid.Row="1"
                              HorizontalAlignment="Left">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" />
                                    <TextBlock Text="{Bin开发者_JS百科ding Item.NomeRazaoSocial}" />
                                </StackPanel>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

But when I change the CheckBox on the ComboBoxItem, it does not reflect on the property.

The code for DB_Aux_Pessoas is below:

[MetadataTypeAttribute(typeof(DB_Aux_Pessoas.DB_Aux_PessoasMetadata))]
public partial class DB_Aux_Pessoas
{

    // This class allows you to attach custom attributes to properties
    // of the DB_Aux_Pessoas class.
    //
    // For example, the following marks the Xyz property as a
    // required property and specifies the format for valid values:
    //    [Required]
    //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
    //    [StringLength(32)]
    //    public string Xyz { get; set; }
    internal sealed class DB_Aux_PessoasMetadata
    {

        // Metadata classes are not meant to be instantiated.
        private DB_Aux_PessoasMetadata()
        {
        }

        public Nullable<short> Cliente { get; set; }

        public string Id_Numero { get; set; }

        public string NomeRazaoSocial { get; set; }

        public Nullable<short> Supervisor { get; set; }

        public Nullable<short> Vendedor { get; set; }
    }
}

What I am doing wrong here? Tks in advance.


I can't check this right now, but I'm fairly sure the dependency property IsChecked will only accept a binding to a bool and NOT a Nullable<bool>. You might have to do the conversion in your viewmodel and decide which should be the appropriate default for a null value.


This works fine for me. Add an extra property to give you some visual feedback like this:

public class SelectionItem<T> : ViewModelBase
{
    private bool? _isSelected;
    public bool? IsSelected
    {
        get { return _isSelected; }
        set
        {
            _isSelected = value;
            RaisePropertyChanged("IsSelected");
            RaisePropertyChanged("Feedback");
        }
    }

    public string Feedback
    {
        get
        {
            if (!this.IsSelected.HasValue)
            {
                return "null";
            }

            if (this.IsSelected.Value)
            {
                return "yes";
            }

            return "no";
        }
    }


    public T Item { get; set; }
}

And in your xaml, add an extra TextBlock to show the result:

<ComboBox Grid.Column="1"
                  Grid.Row="1"
                  Height="23"
                  Margin="3,0,0,0"
                  Width="200"
                  HorizontalAlignment="Left"
                  ItemsSource="{Binding Vendedores, Mode=TwoWay}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" />
                        <TextBlock Text="{Binding Item.NomeRazaoSocial}" />
                        <TextBlock Text="{Binding Feedback}" Margin="5,0,0,0" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

If you see "Yes", "No", "Null" changing as you click the checkboxes, then it's working. Your code worked for me.


 public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.RegisterAttached("IsChecked", typeof(bool), typeof(...),
    new PropertyMetadata(false, delegate(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
     ...
    }

IsChecked is bool. You can possibly track BindingExpression Error in Output window.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜