简体   繁体   English

用C#而不是xaml定义资源

[英]Define resources in C# instead of xaml

I would like to access to my staticData using a usercontrol in c#. 我想使用C#中的用户控件访问我的staticData。 But in order to do that i think i need to define resources in C# instead of xaml. 但是为了做到这一点,我认为我需要在C#中定义资源,而不是xaml。

Can anyone help me? 谁能帮我?

Everything in project is working but when i'm trying to access to my table it doesn't return anything because is just defined in xaml. 项目中的所有内容均正常运行,但是当我尝试访问表时,它不会返回任何内容,因为它只是在xaml中定义的。

UserControl: GroupingZoomedInView.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 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;
    }
 }

My Static data 我的静态数据

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);
    }
}

尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在xaml中定义和使用资源,以便它们可以在C#中使用 - How to define and use resources in xaml so they can be used in C# C#资源与XAML资源 - C# Resources vs XAML Resources 如何使用 C# 而不是 XAML 将两个 ResourceDictionary 对象合并到 Application.Current.Resources 中? - How can I merge two ResourceDictionary objects into Application.Current.Resources using C# instead of XAML? 如何将应用程序资源添加到应用程序的 C# 后端而不是 XAML 文件中? - How can I add application resources to my C# backend for the application instead of in the XAML file? XAML-&gt; C#从C#中的资源更新文本属性 - XAML -> C# Update a Text property from Resources in C# 用C#而不是XAML以编程方式处理xmlnodeslist - process xmlnodeslist programatically in c# instead of XAML 来自C#而不是XAML的ConstrainToParentBounds - ConstrainToParentBounds from C# instead of XAML 在 Silverlight 中使用 C# 代替 XAML - Using C# instead of XAML in Silverlight c#WPF XAML RelativeSource对ComboBox.Resources项的引用 - c# WPF XAML RelativeSource reference to ComboBox.Resources item C#XAML-Windows 8.1应用程序多语言资源问题AppBar - C# XAML - Windows 8.1 App Multilingual Resources Issue AppBar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM