изменить объект инфраструктуры Entity перед привязкой к элементу управления WPF - PullRequest
0 голосов
/ 08 марта 2012

Я новичок в WPF и фреймворке сущностей. Я столкнулся со следующей проблемой при работе над приложением. В моем приложении я связываю свои данные, используя источник представления коллекции от платформы лица. одна из моих таблиц базы данных имеет столбец с именем isNumeric datatype Boolean. если это правда, то мой вид сетки в окне WPF должен показывать текст «Числовой» и «Строка», если ложь. Для этого требования я не могу напрямую связать мой результат запроса linq с представлением сетки или любым элементом управления в пользовательском интерфейсе. Любые мысли о том, как я мог решить это. Вот некоторые из моих кодов

MainWindow.XAML.cs code

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private PartNumbersEntities partNumberContext = new PartNumbersEntities();
    private PartNumbersCollection partNumberData;
    //private PartClassesCollection partClassData;
    private CollectionViewSource MasterViewSource;

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //string classFilter = classNameTextBox.Text;
        //if (classFilter.Length == 0)
            classFilter = "Dis";
        //MessageBox.Show(classFilter);
        var result = partNumberContext.PartNumbers.Where(p => p.PartClass.chrPCName.Contains(classFilter)).Select(p => p);    

        this.partNumberData = new PartNumbersCollection(result, partNumberContext);
        this.MasterViewSource = (CollectionViewSource)this.FindResource("MasterView");
        this.MasterViewSource.Source = this.partNumberData;           
    }

}

Моя частьNumber Collection

class PartNumbersCollection : ObservableCollection<PartNumber>
{
    private PartNumbersEntities _context;
    public PartNumbersEntities Context
    {
        get { return _context; }
    }


    public PartNumbersCollection(IEnumerable<PartNumber> partNumbers, PartNumbersEntities context)
        : base(partNumbers)
    {
        _context = context;

    }
}

Код Xaml

<Window x:Class="Engenious.PartNumbersUI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="454" Width="1033" Loaded="Window_Loaded">

<Window.Resources>
    <CollectionViewSource x:Key="MasterView" />
    <CollectionViewSource x:Key="PartProperties" 
        Source="{Binding Source={StaticResource MasterView}, 
        Path='PartProperties'}"/>
    <!--<CollectionViewSource x:Key="PartNumberView" 
        Source="{Binding Source={StaticResource MasterView}, 
        Path='PartNumbers'}"/>-->
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource MasterView}}">
    <Grid.RowDefinitions>
        <RowDefinition Height="42" />
        <RowDefinition Height="310" />
        <RowDefinition Height="42" />            
    </Grid.RowDefinitions>
    <Grid Grid.Row="0" Name="Grid0">
        <StackPanel Name="StackPanel1" Orientation="Horizontal">
            <Label Content="Class Filter" Height="28" Name="label1" Margin="3" />
            <TextBox Height="28" Name="classNameTextBox" Width="120" Margin="3"/>
            <Button Content="Apply" Height="28" Name="applyButton" Width="80" Margin="3"/>
        </StackPanel>
    </Grid>
    <ListView Grid.Row="1" Name="ListView1" VerticalContentAlignment="Top" 
              VerticalAlignment="Top" HorizontalAlignment="Left" Height="310" 
              Width="320"
              IsSynchronizedWithCurrentItem="True"
              ItemsSource="{Binding }">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Part Class Name" Width="150">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=PartClass.chrPCName}"  Margin="-6,0,-6,0"/>                                
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>                    

                <GridViewColumn Header="Part Number" Width="130">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <!--<Label Content="{Binding Source={StaticResource PartNumberView},Path=chrPNPartNumber}" Margin="-6,0,-6,0"/>-->
                            <Label Content="{Binding Path=chrPNPartNumber}" Margin="-6,0,-6,0"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

            </GridView>
        </ListView.View>
    </ListView>

    <ListView Grid.Row="1" Height="310" HorizontalAlignment="Left" Margin="350,0,0,0" Name="listView2"
              VerticalAlignment="Top" Width="375"
              IsSynchronizedWithCurrentItem="True"
              ItemsSource="{Binding Source={StaticResource  PartProperties}}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" Width="150">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=ConfigurationProperty.chrCPProperty}"  Margin="-6,0,-6,0"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn Header="Datatype" Width="130">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=ConfigurationProperty.bitCPIsNumeric}" Margin="-6,0,-6,0"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

            </GridView>
        </ListView.View>




    </ListView>
    <StackPanel Name="StackPanel4" Orientation="Horizontal" Grid.Row="2">
        <Button Height="25" Name="btnAddDetail" Width="82" Margin="3">Save</Button>
        <Button Height="26" Name="btnDeleteDetail" Width="83" Margin="3">Delete</Button>
    </StackPanel>

</Grid>

Вот скриншот enter image description here

1 Ответ

0 голосов
/ 09 марта 2012

Я использовал Data Trigger для решения этой проблемы .. Вот мой XAML для этого.

<GridViewColumn Header="Datatype" Width="130">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate >

                            <Label>
                                <Label.Resources>
                                    <Style TargetType="{x:Type Label}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path = 'ConfigurationProperty.bitCPIsNumeric'}" Value="True">
                                                <Setter Property="Content" Value="Numeric" />
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Path = 'ConfigurationProperty.bitCPIsNumeric'}" Value="False">
                                                <Setter Property="Content" Value="String" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Label.Resources>
                            </Label>

                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
...