2012-03-08 44 views
0

我是WPF和實體框架的新手。在處理應用程序時遇到以下問題。在我的應用程序中,我使用實體框架中的集合視圖源來綁定我的數據。我的一個數據庫表中有一個名爲isNumeric datatype Boolean的列。如果它是真的,那麼我在WPF窗口中的網格視圖應該顯示文本「Numeric」和「String」(如果爲false)。對於這個需求我不能直接綁定我的linq查詢結果與網格視圖或UI中的任何控件。任何想法我怎麼能解決這個問題。 這裏我的一些代碼在綁定到WPF控件之前修改實體框架對象

MainWindow.XAML.cs代碼

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;   
    } 

} 

我PARTNUMBER收藏

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

+0

使用2個TextBlocks,其中一個帶有「Numeric」,另一個帶有「String」。在isNumeric上使用樣式,觸發器和綁定來設置可見性。 – 2012-03-08 16:22:08

+0

@jberger,非常感謝。有效。 – IamaC 2012-03-08 19:33:48

+0

這將是一個很好的發佈你的最終結果作爲答案,然後標記爲正確 – 2012-03-08 22:21:41

回答

0

我已經使用數據觸發器來解決這個問題.. 這是我的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>