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>
他重新是一个屏幕快照
使用2个TextBlocks,其中一个带有“Numeric”,另一个带有“String”。在isNumeric上使用样式,触发器和绑定来设置可见性。 – 2012-03-08 16:22:08
@jberger,非常感谢。有效。 – IamaC 2012-03-08 19:33:48
这将是一个很好的发布你的最终结果作为答案,然后标记为正确 – 2012-03-08 22:21:41