I have an enum that I need to bind into a ComboBox . The ComboBox is located inside the data template tag . How can I bind the enum into the ComboBox?
This is the enum:
public enum Status
{
Enable,
Disable
}
This is the xaml:
<Window.Resources>
<cv:StatusToBooleanConverter x:Key="statusToBooleanConverter"/>
<ObjectDataProvider x:Key="dataFromEnum" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="enum:Status"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<DataTemplate>
<StackPanel>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}},
Path=DataContext.Statusstring}" x:Name="cbProductionLineStatus"
FlowDirection="LeftToRight" FontSize="16" Foreground="MidnightBlue"
HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
Here is the viewmodel code:
public List<Status> status;
public List<Status> Statusstring
{
get
{
foreach (List<Status> iColor in System.Enum.GetValues(typeof(Status)))
{
status = iColor;
}
return status;
}
}
I tried implementing the Find ancestor method half way and got stucked.
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}},
Path=?}" SelectedValue="{Binding ProductionLineStatus,Mode=TwoWay}" SelectedValuePath="ProductionLineStatus" DisplayMemberPath="ProductionLineStatus" x:Name="cbProductionLineStatus" FlowDirection="LeftToRight" FontSize="16" Foreground="MidnightBlue"
HorizontalAlignment="Stretch" VerticalAlignment="Center" />
I am trying to populate the enum status into my combobox but it is failing. However, now I am trying to implement as the solution stated by Steven but still its not working.
I able to solve this problem by implementing view model like this:
public Status status = Status.Enable;
public List<string> Statusstring {
get
{
return System.Enum.GetNames(typeof(Status)).ToList();
}
}
This is my xaml:
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}},
Path=DataContext.Statusstring}" SelectedValue="{Binding ProductionLineStatus, Converter={StaticResource statusToBooleanConverter}, Mode=TwoWay}" x:Name="cbProductionLineStatus" FlowDirection="LeftToRight" FontSize="16" Foreground="MidnightBlue"
HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
I think I would have solved it by writing a property, and using that property in the Combobox:
Something similair like this:
public Status status = Status.Enable;
public string Statusstring
{
get
{
if (status == Status.Enable)
return "Enable";
else
return "Disable";
}
}
I think the best solution would be to implement the view model as answered by anonymous_apple. Then in the combo box you could further have a data template that contains a TextBlock. And in the Textblock you'd set the Text as so: Text="{Binding }".
^^This solved my issue, it should solve yours as well..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.