2012-03-08 6 views
0

WPF 및 엔티티 프레임 워크를 처음 사용합니다. 응용 프로그램에서 작업하는 동안 다음 문제가 발생했습니다. 내 응용 프로그램에서 엔티티 프레임 워크의 컬렉션 뷰 소스를 사용하여 데이터를 바인딩하고 있습니다. 내 데이터베이스 테이블 중 하나는 isNumeric 데이터 유형 부울이라는 열이 있습니다. 그게 사실이라면 WPF 윈도우의 그리드 뷰는 "Numeric"과 "String"을 false로 표시해야합니다. 이 요구 사항에 대해 필자는 직접 linq 쿼리 결과를 그리드보기 또는 UI의 모든 컨트롤과 바인딩하지 않습니다. 어떻게하면이 문제를 해결할 수 있을지에 대한 생각.WPF 컨트롤에 바인딩하기 전에 Entity Framework 객체를 수정하십시오.

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 개의 TextBlock을 사용합니다. 하나는 "숫자"이고 다른 하나는 "문자열"입니다. Visibility를 설정하려면 isNumeric에서 Style, Triggers 및 Binding을 사용하십시오. –

+0

@jberger, 정말 고마워요. 그것은 효과가 있었다. – IamaC

+0

최종 결과를 대답으로 게시 한 다음 올바른 것으로 표시하는 것이 좋습니다 –

답변

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> 
관련 문제