开发者

ComboBox - bind selected value

I want to bind the combobox's selected value to the bounded object property, and also set the selected index of every combobox to 0.

The problem is that only first combo shows the selected item.

public enum SubEnum1
{       
   Apple=1,
   Banana=2,
   Pear=3
}    

public enum FullEnum
{       
   Apple=1,
   Banana=2,
   Pear=3,

   Cucumber=4,
   Tomato=5,
   Onion=6
}

Im XAML window i have some data control (list) where is a datatemplate that has a combobox.

comboboxes are bounded to SubEnum1.

The data-control is bounded to an object-collection:

List<MyObject> collection = new List<MyObject>()
//collection.Add...

mylist.ItemsSource = collection;

public class MyObject
{
  public FullEnum TheSelectedEnum {get;set;}
  ....
  //other properties
}



public class EnumConverter2 : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            return value;

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
                return (FullEnum)value;

            else return "";

        }
    }
<ObjectDataProvider x:Key="Enum1"
                    MethodName="GetValues" 
                    ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:SubEnum1" />
            </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

<ListBox Height="261" HorizontalAlignment="Left" Name="mylist" VerticalAlignment="Top" Width="278">
   <ListBox.ItemTemplate>
      <DataTemplate>
          <StackPanel>              
              <ComboBox Height="23" Width="90"                                
                        ItemsSource="{Binding Source={StaticResource En开发者_JS百科um1}}"  
                        SelectedValue="{Binding Path=TheSelectedEnum, Converter={StaticResource enumConverter}}"
                        SelectedIndex="0"/> 
          </StackPanel>
      </DataTemplate>
</ListBox.ItemTemplate>     

</ListBox>

ComboBox - bind selected value

(If I expand other comboboxes I see the values)

Update to post:

Maybe I can somehow else pass the selected value to the binded object?


You can do this and solve your problem:

public enum SubEnum1 
{           
    None=0,     
    Apple=1,    
    Banana=2,    
    Pear=3 
} 

Then use FallbackValue:

<ComboBox Height="23" Width="90"
    ItemsSource="{Binding Source={StaticResource Enum1}}"
    SelectedValue="{Binding Path=TheSelectedEnum, FallbackValue=0}" /> 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜