English 中文(简体)
从C# WPF的代码中引入静态资源
原标题:Initializing static resource from code in C# WPF app

我有两个WPF窗口。 主要包括一个电离层到<代码>。 ObservableCollection<Person>。 我可以在名单上添加和删除物体(人民)。 我在修改一个人时还可以展示另一个窗口。

个人有三个财产: 姓名、年老和年龄,并适当实施。 在新的窗口中,我有3个文字箱,与固定资源人“人”联系在一起。

当我开始启用新窗口时,我向施工者表示反对,然后我希望这个人财产在三个文本框中显示。

下面的法典认为,这一切都是适当的:

public ModifyPerson(Person modPerson)  
{  
    // ... some code  
    Person p = this.Resources["person"] as Person;  
    p.Name = modPerson.Name;  
    p.LastName = modPerson.LastName;  
    p.Age = modPerson.Age;  
}  

但我更喜欢这样做:

public ModifyPerson(Person modPerson)  
{  
    // ... some code  
    this.Resources["person"] = modPerson;  
}

但是,这并不奏效。 (资源是适当分配的,但文本箱没有说明变人财产的价值。)

如何解决这一问题?

问题回答

页: 1 而不是使用<代码>StaticResource 将该财产置于你所约束的财产之中。

public ModifyPerson(Person modPerson)
{
    personToModify = modPerson;
}

private Person personToModify;

public Person PersonToModify
{
    get
    {
        return personToModify;
    }
}

And XAML:

<StackPanel DataContext="{Binding PersonToModify}">
    <TextBox Text="{Binding Name, Mode=TwoWay" />
    <TextBox Text="{Binding LastName, Mode=TwoWay" />
    <TextBox Text="{Binding Age, Mode=TwoWay" />
</StackPanel>

(为了简明扼要,我没有标签)

您可使用<代码>DynamicResource,而不是StaticResource,但使用您的<代码>Model中的任何一种语文,其实际用途为t,而您应使用Bled

<TextBox Text="{Binding Nameiii"></TextBox>
<TextBox Text="{Binding LastNameiii"></TextBox>
<TextBox Text="{Binding Ageiii"></TextBox>

in Code: u need a copy for UI binding, like this,

public EditPlayerWindow(PlayerBO arg)
        : this() {
        this.source =arg;
        this.view = new PlayerBO();
        arg.CopyTo(this.view);
        this.DataContext = view;
    iii

页: 1 同样:

public void CopyTo(PlayerBO player) {
player.Id = this.Id;
player.Name = this.Name;
player.CurrentPolicyVersion = this.CurrentPolicyVersion;
player.CreatedAt = this.CreatedAt;
player.UpdatedAt = this.UpdatedAt;
player.Description = this.Description;
player.MACAddress = this.MACAddress;
player.IPAddress = this.IPAddress;
player.Policy = Policy;

iii

最后:

view.CopyTo(source);

然后节省来源。

愿它帮助!





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签