2009-07-16 15 views
10

नहीं पाया जा सकता है मैं गतिशील मेरे GridView में कॉलम जोड़ने के लिए निम्न कोड का उपयोग कर रहा: मेंगतिशील रूप से जोड़ा DataTemplate - कन्वर्टर के लिए StaticResource

public void AddGridViewColumns() 
{ 
    GridView view = (GridView)_myListView.View; 
    GridViewColumn column = BuildGridViewColumn(1); 
    view.Columns.Add(column); 
} 

public virtual GridViewColumn BuildGridViewColumn(int blockIndex) 
{ 
    string templateXaml = string.Format(
     @"<DataTemplate 
      xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
      xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" 
      xmlns:local=""clr-namespace:Pse.MyTest;assembly=MyTest""> 
      <DataTemplate.Resources> 
       <local:BlockToBrushConverter 
        x:Key=""_blockToBrushConverter"" /> 
      </DataTemplate.Resources> 
      <TextBlock 
       Style=""{{StaticResource _gridViewCenterItemStyle}}"" 
       Text=""{{Binding Path=Block{0}.ConditionLabel}}"" 
       Foreground=""{{Binding Path=Block{0}, Converter={{StaticResource _blockToBrushConverter}}}}"" /> 
     </DataTemplate>", 
     blockIndex); 
    System.Diagnostics.Debug.Print(templateXaml); 
    GridViewColumn column = new GridViewColumn(); 
    column.Header = string.Format("Block {0}", blockIndex); 
    column.CellTemplate = (DataTemplate)XamlReader.Parse(templateXaml); 
    return column; 
} 

Debug.Print परिणाम:

<DataTemplate 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Pse.MyTest;assembly=MyTest"> 
    <DataTemplate.Resources> 
     <local:BlockToBrushConverter 
      x:Key="_blockToBrushConverter" /> 
    </DataTemplate.Resources> 
    <TextBlock 
     Style="{StaticResource _gridViewCenterItemStyle}" 
     Text="{Binding Path=Block1.ConditionLabel}" 
     Foreground="{Binding Path=Block1, Converter={StaticResource _blockToBrushConverter}}" /> 
</DataTemplate> 

जब मैं इसे चलाता हूं, तो यह कहता है कि यह _blockToBrushConverter नहीं ढूंढ सकता है।

अपवाद है:

System.Windows.Markup.XamlParseException was unhandled 
    Message="Cannot find resource named '{_blockToBrushConverter}'. Resource names are case sensitive. Error at object 'System.Windows.Data.Binding' in markup file 'MyTest;component/SetupWindowDictionary.xaml' Line 23 Position 13." 
    Source="PresentationFramework" 
    LineNumber=23 
    LinePosition=13 
    NameContext="Foreground" 

अगर मैं ForeColor विशेषता निकालने के लिए, XAML ठीक पार्स करता है (और मैं उम्मीद परिणाम प्राप्त)। ऐसा लगता है कि डेट टेम्पलेट में कनवर्टर भी शामिल है। स्रोतों को यह गारंटी देनी चाहिए कि यह पाया जाएगा, लेकिन किसी कारण से यह नहीं है।

मैंने पेड़ (विंडो स्तर पर) में मेरे कनवर्टर संसाधन के लिए घोषणा को स्थानांतरित करने का भी प्रयास किया, लेकिन यह भी काम नहीं करता था। ऐसा लगता है कि "_gridViewCenterItemStyle" ढूंढने में सक्षम है, लेकिन किसी कारण से, यह कनवर्टर खोजने से इंकार कर देता है।

किसी भी विचार की सराहना की।


यहाँ मामले में स्टैक ट्रेस के साथ पूरे अपवाद यह मदद करता है:

System.Windows.Markup.XamlParseException was unhandled 
    Message="Cannot find resource named '{_blockToBrushConverter}'. Resource names are case sensitive. Error at object 'System.Windows.Data.Binding' in markup file 'MyTest;component/SetupWindowDictionary.xaml' Line 23 Position 13." 
    Source="PresentationFramework" 
    LineNumber=23 
    LinePosition=13 
    NameContext="Foreground" 
    StackTrace: 
     at System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition, Uri baseUri, XamlObjectIds currentXamlObjectIds, XamlObjectIds contextXamlObjectIds, Type objectType) 
     at System.Windows.Markup.XamlParseException.ThrowException(ParserContext parserContext, Int32 lineNumber, Int32 linePosition, String message, Exception innerException) 
     at System.Windows.Markup.BamlRecordReader.ThrowExceptionWithLine(String message, Exception innerException) 
     at System.Windows.Markup.BamlRecordReader.ThrowException(String id, String parameter) 
     at System.Windows.StaticResourceExtension.ProvideValueInternal(IBamlReader bamlReader, Object targetObject, Object targetProperty, Boolean allowDeferredReference) 
     at System.Windows.StaticResourceExtension.ProvideValue(IServiceProvider serviceProvider) 
     at System.Windows.Markup.BamlRecordReader.ProvideValueFromMarkupExtension(MarkupExtension markupExtension, Object obj, Object member) 
     at System.Windows.Markup.BamlRecordReader.BaseReadOptimizedMarkupExtension(Object element, Int16 attributeId, PropertyDefinition propertyDefinition, Object value) 
     at System.Windows.Markup.BamlRecordReader.ReadPropertyWithStaticResourceIdRecord(BamlPropertyWithStaticResourceIdRecord bamlPropertyWithStaticResourceIdRecord) 
     at System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord) 
     at System.Windows.StyleHelper.LoadOptimizedTemplateContent(DependencyObject container, ParserContext parserContext, OptimizedTemplateContent optimizedTemplateContent, FrameworkTemplate frameworkTemplate, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField) 
     at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField) 
     at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate) 
     at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container) 
     at System.Windows.FrameworkElement.ApplyTemplate() 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at System.Windows.Controls.GridViewRowPresenter.MeasureOverride(Size constraint) 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at System.Windows.Controls.Border.MeasureOverride(Size constraint) 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at System.Windows.Controls.Control.MeasureOverride(Size constraint) 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint) 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) 
     at System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint) 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) 
     at System.Windows.Controls.ScrollContentPresenter.MeasureOverride(Size constraint) 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at System.Windows.Controls.DockPanel.MeasureOverride(Size constraint) 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) 
     at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) 
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint) 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at System.Windows.Controls.ScrollViewer.MeasureOverride(Size constraint) 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at System.Windows.Controls.Border.MeasureOverride(Size constraint) 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at System.Windows.Controls.Control.MeasureOverride(Size constraint) 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint) 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) 
     at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) 
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
     at System.Windows.UIElement.Measure(Size availableSize) 
     at System.Windows.ContextLayoutManager.UpdateLayout() 
     at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg) 
     at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork() 
     at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() 
     at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) 
     at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) 
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter) 
     at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) 
     at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) 
     at System.Windows.Threading.DispatcherOperation.InvokeImpl() 
     at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) 
     at System.Threading.ExecutionContext.runTryCode(Object userData) 
     at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Windows.Threading.DispatcherOperation.Invoke() 
     at System.Windows.Threading.Dispatcher.ProcessQueue() 
     at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter) 
     at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) 
     at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) 
     at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter) 
     at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg) 
     at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
     at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
     at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
     at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
     at System.Windows.Threading.Dispatcher.Run() 
     at System.Windows.Application.RunDispatcher(Object ignore) 
     at System.Windows.Application.RunInternal(Window window) 
     at System.Windows.Application.Run(Window window) 
     at System.Windows.Application.Run() 
     at Pse.TestExperiment.App.Main() in C:\Documents and Settings\Daniel Manes\My Documents\Projects\Cmd21\Team Map Experiment 2\Solution\TestExperiment\obj\Debug\App.g.cs:line 0 
     at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 

संपादित करें:

समस्या हल: मैं अपने पूरी परियोजना के माध्यम से एक खोज किया था और पाया है मैं एक था BlockToBrushConverter संसाधन घोषणा की अतिरिक्त प्रति एक ही एक्स के साथ: मेरे संसाधन शब्दकोशों में से एक में कुंजी। एक बार जब मैंने नकल को समाप्त कर दिया, तो समस्या गायब हो गई।

+0

मुझे आपके डीबग प्रिंट के समान जेनरेट किया गया डेटा टेम्पलेट मिला है - जब मैं इसे लोड करता हूं, तो मुझे लगता है कि "अनुलग्नक संपत्ति संसाधन प्रकार डेटाटेम्प्लेट में नहीं मिला" हालांकि मेरे नामस्थान में शामिल है और मेरा बाकी xaml समान है नियंत्रण – jchristof

उत्तर

6

किसी और मामले में किसी दिन इस प्रश्न को देखने पर, यह पता चला है कि, इस प्रश्न के शीर्ष पर प्रस्तुत कोड वास्तव में काम करता है। बग कि मैं निम्नलिखित घोषणा मेरी XAML में कहीं और था:

<local:BlockToBrushConverter 
    x:Key="_blockToBrushConverter" /> 

मेरा मानना ​​है कि इस दोहराव दो बार गतिशील DataTemplate के लिए स्थानीय स्तर पर घोषित किए जाने से संसाधन (यह एक ही नाम के साथ एक संसाधन नहीं बना सका रोका भीतर वही संदर्भ)।

एक चीज जो मेरे लिए अनसुलझा है, क्यों कुछ स्टेटिक स्रोतों को स्थानीय रूप से घोषित करने की आवश्यकता है, जबकि अन्य (जैसे _gridViewCenterItemStyle StaticResource) को बिना किसी समस्या के पेड़ (जैसे विंडो स्तर पर) घोषित किया जा सकता है।

संबंधित मुद्दे