Browse Source

Все ComboBox доведены до ума.

master
Никита 6 years ago
parent
commit
063337e2a2
  1. 6
      eCompanies/App.xaml.cs
  2. 18
      eCompanies/Contract.cs
  3. 52
      eCompanies/EnumBindingSourceExtension.cs
  4. 34
      eCompanies/EnumDescriptoionTypeConverter.cs
  5. 11
      eCompanies/MainWindow.xaml
  6. 5
      eCompanies/UsersWindow.xaml
  7. 2
      eCompanies/eCompanies.csproj

6
eCompanies/App.xaml.cs

@ -1,12 +1,8 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Configuration;
using System.Data; using System.Data;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Navigation;
namespace eCompanies namespace eCompanies
{ {

18
eCompanies/Contract.cs

@ -1,12 +1,18 @@
using System; using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace eCompanies namespace eCompanies
{ {
public enum Status { NotReachedYet, Reached, Terminated } [TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum Status
{
[Description("Еще не заключен")]
NotReachedYet,
[Description("Заключен")]
Reached,
[Description("Расторгнут")]
Terminated
}
public class Contract public class Contract
{ {
private Companies c; private Companies c;

52
eCompanies/EnumBindingSourceExtension.cs

@ -0,0 +1,52 @@
using System;
using System.Windows.Markup;
namespace eCompanies
{
public class EnumBindingSourceExtension : MarkupExtension
{
private Type _enumType;
public Type EnumType
{
get { return this._enumType; }
set
{
if (value != this._enumType)
{
if (null != value)
{
Type enumType = Nullable.GetUnderlyingType(value) ?? value;
if (!enumType.IsEnum)
throw new ArgumentException("Type must be for an Enum.");
}
this._enumType = value;
}
}
}
public EnumBindingSourceExtension() { }
public EnumBindingSourceExtension(Type enumType)
{
this.EnumType = enumType;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (null == this._enumType)
throw new InvalidOperationException("The EnumType must be specified.");
Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
Array enumValues = Enum.GetValues(actualEnumType);
if (actualEnumType == this._enumType)
return enumValues;
Array tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1);
enumValues.CopyTo(tempArray, 1);
return tempArray;
}
}
}

34
eCompanies/EnumDescriptoionTypeConverter.cs

@ -0,0 +1,34 @@
using System;
using System.ComponentModel;
using System.Reflection;
namespace eCompanies
{
public class EnumDescriptionTypeConverter : EnumConverter
{
public EnumDescriptionTypeConverter(Type type)
: base(type)
{
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
if (value != null)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
if (fi != null)
{
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return ((attributes.Length > 0) && (!String.IsNullOrEmpty(attributes[0].Description))) ? attributes[0].Description : value.ToString();
}
}
return string.Empty;
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}

11
eCompanies/MainWindow.xaml

@ -4,17 +4,8 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:eCompanies" xmlns:local="clr-namespace:eCompanies"
xmlns:core="clr-namespace:System;assembly=mscorlib"
xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ObjectDataProvider x:Key="Status" MethodName="GetValues" ObjectType="{x:Type core:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type Type="local:Status"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid> <Grid>
<DataGrid x:Name="companyGrid" HorizontalAlignment="Left" Height="354" Margin="10,55,0,0" VerticalAlignment="Top" Width="772" ItemsSource="{Binding}" AutoGenerateColumns="False" MouseDoubleClick="companyGrid_MouseDoubleClick"> <DataGrid x:Name="companyGrid" HorizontalAlignment="Left" Height="354" Margin="10,55,0,0" VerticalAlignment="Top" Width="772" ItemsSource="{Binding}" AutoGenerateColumns="False" MouseDoubleClick="companyGrid_MouseDoubleClick">
<DataGrid.Columns> <DataGrid.Columns>
@ -22,7 +13,7 @@
<DataGridTextColumn Header="Название компании" Binding="{Binding Name}" Width="*"/> <DataGridTextColumn Header="Название компании" Binding="{Binding Name}" Width="*"/>
<DataGridComboBoxColumn x:Name="ContractComboBox" Header="Состояние контракта" <DataGridComboBoxColumn x:Name="ContractComboBox" Header="Состояние контракта"
SelectedValueBinding="{Binding Path=contract.ContractStatusId}" SelectedValueBinding="{Binding Path=contract.ContractStatusId}"
ItemsSource="{Binding Source={StaticResource Status}}"> ItemsSource="{Binding Source={local:EnumBindingSource {x:Type local:Status}}}">
</DataGridComboBoxColumn> </DataGridComboBoxColumn>
</DataGrid.Columns> </DataGrid.Columns>
</DataGrid> </DataGrid>

5
eCompanies/UsersWindow.xaml

@ -20,10 +20,7 @@
</DataGrid.Columns> </DataGrid.Columns>
</DataGrid> </DataGrid>
<TextBox x:Name="companyNameBox" HorizontalAlignment="Left" Height="40" Margin="10,10,0,0" TextWrapping="Wrap" Text="none" VerticalAlignment="Top" Width="470" FontSize="22"/> <TextBox x:Name="companyNameBox" HorizontalAlignment="Left" Height="40" Margin="10,10,0,0" TextWrapping="Wrap" Text="none" VerticalAlignment="Top" Width="470" FontSize="22"/>
<ComboBox x:Name="contractStatusComboBox" HorizontalAlignment="Left" Height="40" Margin="490,10,0,0" VerticalAlignment="Top" Width="292" FontSize="22"> <ComboBox x:Name="contractStatusComboBox" HorizontalAlignment="Left" Height="40" Margin="490,10,0,0" VerticalAlignment="Top" Width="292" FontSize="22" ItemsSource="{Binding Source={local:EnumBindingSource {x:Type local:Status}}}">
<ComboBoxItem>Еще не заключен</ComboBoxItem>
<ComboBoxItem>Заключен</ComboBoxItem>
<ComboBoxItem>Расторгнут</ComboBoxItem>
</ComboBox> </ComboBox>
</Grid> </Grid>
</Window> </Window>

2
eCompanies/eCompanies.csproj

@ -140,6 +140,8 @@
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="CompaniesDBContext.cs" /> <Compile Include="CompaniesDBContext.cs" />
<Compile Include="Contract.cs" /> <Compile Include="Contract.cs" />
<Compile Include="EnumBindingSourceExtension.cs" />
<Compile Include="EnumDescriptoionTypeConverter.cs" />
<Compile Include="Users.cs" /> <Compile Include="Users.cs" />
<Compile Include="UsersWindow.xaml.cs"> <Compile Include="UsersWindow.xaml.cs">
<DependentUpon>UsersWindow.xaml</DependentUpon> <DependentUpon>UsersWindow.xaml</DependentUpon>

Loading…
Cancel
Save