2013-03-14 52 views
0

我想使用c#中的usercontrol訪問我的staticData。但爲了做到這一點,我認爲我需要用C#代替xaml來定義資源。在C#中定義資源而不是xaml

任何人都可以幫助我嗎?

項目中的一切正在工作,但是當我試圖訪問我的表時,它不返回任何東西,因爲它只是在xaml中定義的。

用戶控件: GroupingZoomedInView.xaml

<UserControl 
x:Class="CaiMU_Professor.Grouping.GroupingZoomedInView" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:CaiMU_Professor.Grouping.Data" 
xmlns:telerikGrid="using:Telerik.UI.Xaml.Controls.Grid" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignHeight="300" 
d:DesignWidth="400"> 

<Grid> 
    <Grid.Resources> 
     <local:PeopleViewModel x:Key="Model"/> 
    </Grid.Resources> 
    <telerikGrid:RadDataGrid x:Name="dataGrid" ItemsSource="{Binding Data,Source={StaticResource Model}}" AutoGenerateColumns="False" FontSize="{StaticResource ControlContentThemeFontSize}" SelectionUnit="Row"> 
     <telerikGrid:RadDataGrid.GroupDescriptors> 
      <telerikGrid:DelegateGroupDescriptor> 
       <telerikGrid:DelegateGroupDescriptor.KeyLookup> 
        <local:AlpabeticGroupKeyLookup/> 
       </telerikGrid:DelegateGroupDescriptor.KeyLookup> 
      </telerikGrid:DelegateGroupDescriptor> 
     </telerikGrid:RadDataGrid.GroupDescriptors> 
     <telerikGrid:RadDataGrid.Columns> 
      <telerikGrid:DataGridTextColumn PropertyName="Template"/> 
      <telerikGrid:DataGridTextColumn PropertyName="data"/> 
      <telerikGrid:DataGridTextColumn PropertyName="info"/> 
      <telerikGrid:DataGridTextColumn PropertyName="score"/> 
      <telerikGrid:DataGridTextColumn PropertyName="result"/> 
      <telerikGrid:DataGridTextColumn PropertyName="repeats"/> 
     </telerikGrid:RadDataGrid.Columns> 
    </telerikGrid:RadDataGrid> 
</Grid> 
</UserControl> 

GroupingZoomedInView.xaml.cs

public sealed partial class GroupingZoomedInView : UserControl, ISemanticZoomInformation 
{ 
    public String row; 

    public GroupingZoomedInView() 
    { 
     this.InitializeComponent(); 
     this.dataGrid.SelectionChanged += this.SelectionChanged; 
    } 

    public void CompleteViewChangeFrom(SemanticZoomLocation source, SemanticZoomLocation destination) 
    { 
     var list = this.SemanticZoomOwner.ZoomedOutView as GridView; 

     if (list != null) 
     { 
      list.ItemsSource = source.Item; 
     } 
    } 

    public bool IsActiveView {get; set;} 
    public bool IsZoomedInView {get; set;} 
    public SemanticZoom SemanticZoomOwner{get; set;} 

    public void StartViewChangeFrom(SemanticZoomLocation source, SemanticZoomLocation destination) 
    { 
     source.Item = this.dataGrid.GetDataView().Items.OfType<IDataGroup>().Select(c => c.Key); 
    } 

    public void StartViewChangeTo(SemanticZoomLocation source, SemanticZoomLocation destination) 
    { 
     var dataview = this.dataGrid.GetDataView(); 
     var group = dataview.Items.OfType<IDataGroup>().Where(c => c.Key.Equals(source.Item)).FirstOrDefault(); 

     var lastGroup = dataview.Items.Last() as IDataGroup; 
     if (group != null && lastGroup != null) 
     { 
      this.dataGrid.ScrollItemIntoView(lastGroup.ChildItems[lastGroup.ChildItems.Count - 1],() => 
      { 
       this.dataGrid.ScrollItemIntoView(group.ChildItems[0]); 
      }); 
     } 
    } 

    private void SelectionChanged(object sender, DataGridSelectionChangedEventArgs e) 
    { 
     Templates temp = this.dataGrid.SelectedItem as Templates; 
     aux = this.dataGrid.SelectedItem; 
    } 
} 

靜態數據

public class PeopleViewModel 
{ 
    private static List<Templates> staticData; 

    static PeopleViewModel() 
    { 
     Load(); 
    } 

    public IList<Templates> Data 
    { 
     get 
     { 
      return staticData; 
     } 
    } 

    private static void Load() 
    { 
     XMLStuff xml = new XMLStuff(); 
     List<Templates> tempList = xml.getControlOutput(); 
     staticData = new List<Templates>(); 

     foreach (Templates temp in tempList) 
      staticData.Add(temp); 
    } 
} 

回答

0

嘗試

var model = this.Resources["Model] as PeopleViewModel;

+0

它給了我下面的錯誤: 「類型的異常‘System.Runtime.InteropServices.COMException’出現在mscorlib.dll,但在用戶代碼中沒有處理 其他信息:錯誤HRESULT E_FAIL已退還從調用COM組件。「 – Kelianosevis 2013-03-14 18:49:04

+0

@Kelianosevis必須是一個WinRT問題。我曾經使用WPF而不是WinRT(雖然它應該是相同的),所以我不能告訴。您是否嘗試過調試並檢查資源是否存在? – 2013-03-14 18:51:29

+0

隨着一些調試,我發現我的資源實際上並不存在..但我的桌子,據說是正確的,顯示了我正確的結果.. debuuger告訴我,沒有資源..我做錯了什麼? – Kelianosevis 2013-03-14 19:23:40