WPF - 任何人都知道为什么我不能引用这个绑定?

时间:2009-08-18 17:42:03

标签: wpf binding

<StackPanel x:Name="stkWaitingPatients" Width="300" Margin="0,0,0,-3"
   DataContext="{Binding Mode=OneWay, Source={StaticResource local:oPatients}}">

我收到StaticResource引用'local:oPatients'未找到。

以下是代码隐藏:

    public partial class MainWindow : Window
{
    ListBox _activeListBox;
    clsPatients oPatients;

    public MainWindow()
    {
        oPatients = new clsPatients(true);

...

3 个答案:

答案 0 :(得分:2)

为了能够将对象作为StaticResource来处理,它需要位于资源字典中。但是,由于您在MainWindow的构造函数中创建了对象,因此您可以像这样在代码隐藏中设置DataContext

oPatients = new clsPatients(true);
stkWaitingPatients.DataContext = oPatients;

然后将Binding更改为:

{Binding Mode=OneWay}

如果您不打算再次更改DataContext,这是一种不错的做法,否则您需要更灵活的解决方案。

修改:您在评论中提到了ObjectDataProvider。这是你如何做到这一点。首先,为xmlns:sys命名空间的Window添加System(假设您已经为xmlns:local添加了xmlns:sys="clr-namespace:System;assembly=mscorlib" ):

ObjectDataProvider

然后您可以在资源字典中添加<Window.Resources> <ObjectDataProvider x:Key="bindingPatients" ObjectType="{x:Type local:clsPatients}"> <ObjectDataProvider.ConstructorParameters> <sys:Boolean>True</sys:Boolean> </ObjectDataProvider.ConstructorParameters> </ObjectDataProvider> </Window.Resources> ,如下所示:

Binding

使用StaticResource这样的x:Key标记在{Binding Source={StaticResouce bindingPatients}, Mode=OneWay} 中引用它,使用我们在字典中给出的lstWaitingPatients.DataContext = oPatients; 附加属性中指定的相同字符串:

lstWaitingPatients

编辑2:好的,您在答案中发布了更多代码,现在我知道为什么它会在构造函数中抛出异常。你正试图这样做......

this.InitializeComponent()

...但InitializeComponent()直到InitalizeComponent()完成后才真正存在。 Window加载XAML并执行其他一些操作。除非您确实需要在所有这些之前做一些事情,否则在调用Loaded之后或在{{1}}的{​​{1}}的事件处理程序中放置自定义启动代码 事件

答案 1 :(得分:1)

以下设置Code Behind中的ItemsSource并正确处理DataBinding:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        clsPatients oPatients = new clsPatients(true);

        //assuming oPatients implements IEnumerable
        this.lstWaitingPatients.ItemsSource = oPatients;

和XAML:

<ListBox x:Name="lstWaitingPatients"       
         IsSynchronizedWithCurrentItem="true"
         ItemTemplate="{StaticResource WaitingPatientsItemTemplate}"
         FontSize="21.333" Height="423.291"
         ScrollViewer.VerticalScrollBarVisibility="Visible"
         GotFocus="lstWaitingPatients_GotFocus"
         />

答案 2 :(得分:0)

现在,我无法让它工作......我得到了一般的Windows启动错误。

以下是使用Initializer和实例化类的代码隐藏:

    public partial class MainWindow : Window
{
    ListBox _activeListBox;

    public MainWindow()
    {
        clsPatients oPatients = new clsPatients(true);
        lstWaitingPatients.DataContext = oPatients;

        this.InitializeComponent();

这是我的XAML的顶部:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Orista_Charting"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" 
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
x:Class="Orista_Charting.MainWindow"
x:Name="windowMain"
Title="Orista Chart"
Width="1024" Height="768" Topmost="True" WindowStartupLocation="CenterScreen" Activated="MainWindow_Activated" >


<Window.Resources>        

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/ButtonStyles.xaml"/>
            <ResourceDictionary Source="Resources/OtherResources.xaml"/>
            <ResourceDictionary Source="Resources/TextBlockStyles.xaml"/>
            <ResourceDictionary Source="Resources/Converters.xaml"/>
        </ResourceDictionary.MergedDictionaries>

这是相关的XAML,如您所见,我继续将DataContext从StackPanel移动到ListBox。这不会运行,但它会在设计视图中呈现(但是,ListBox中没有数据):

<!-- Waiting Patients List -->
    <Border BorderThickness="1,1,1,1" BorderBrush="#FF000000" Padding="10,10,10,10" 
            CornerRadius="10,10,10,10" Background="#FFFFFFFF" Margin="15.245,187.043,0,41.957" HorizontalAlignment="Left" >
        <StackPanel x:Name="stkWaitingPatients" Width="300" Margin="0,0,0,-3"> 

            <StackPanel Orientation="Horizontal">    
                <TextBlock Text="Waiting Patients:" VerticalAlignment="Center" FontSize="21.333" Margin="0,0,0,20"/>
                <TextBlock HorizontalAlignment="Right" Margin="0,0,38.245,0" Width="139" Height="16" 
                Text="Minutes Waiting" TextWrapping="Wrap" Foreground="#FF9C2525" FontWeight="Bold" VerticalAlignment="Bottom"
                TextAlignment="Right"/> 
                <!-- Too be implemented, this is the wait animation -->
                <!--<Image x:Name="PollGif" Visibility="{Binding Loading}"
                       HorizontalAlignment="Left" Margin="100,0,0,0" Width="42.5" Height="42.5" 
                       Source="Images/loading-gif-animation.gif" Stretch="Fill"/>-->       
            </StackPanel>                  

            <ListBox x:Name="lstWaitingPatients"       
                     DataContext="{Binding Mode=OneWay}" ItemsSource="{Binding Mode=OneWay}"
                     IsSynchronizedWithCurrentItem="true"
                     ItemTemplate="{StaticResource WaitingPatientsItemTemplate}"
                     FontSize="21.333" Height="423.291" ScrollViewer.VerticalScrollBarVisibility="Visible"
                     GotFocus="lstWaitingPatients_GotFocus"
                     />

        </StackPanel>
    </Border>

好的,但是如果我只是在代码隐藏中注释掉分配行,它就会运行(尽管列表框中没有数据):

    public partial class MainWindow : Window
{
    ListBox _activeListBox;

    public MainWindow()
    {
        clsPatients oPatients = new clsPatients(true);
        //lstWaitingPatients.DataContext = oPatients;

谢谢!