2013-03-14 2 views
0

Я хотел бы получить доступ к моей staticData, используя usercontrol в C#. Но для этого я считаю, что мне нужно определить ресурсы на C# вместо xaml.Определить ресурсы в C# вместо xaml

Может ли кто-нибудь мне помочь?

Все в проекте работает, но когда я пытаюсь получить доступ к моей таблице, он ничего не возвращает, потому что он определен только в xaml.

UserControl: 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

+0

@ Kelianosevis, который должен быть проблемой WinRT. Я использовал для работы с WPF не WinRT (хотя он должен быть таким же), поэтому я не мог сказать. Вы пробовали отлаживать и проверять ресурс на самом деле там? –

+0

С некоторой отладкой я обнаружил, что мой ресурс на самом деле там нет. Но моя таблица, которая предположительно была правильной, показывает мне правильные результаты. Дебюгер говорит мне, что ресурсов нет. Что я делаю неправильно? – Kelianosevis

Смежные вопросы