简体   繁体   English

以编程方式更改 xaml 的内容以更改控件的属性

[英]Programmatically change content of xaml to change property of a control

Is it's possible to change xaml programmatically through c#?是否可以通过 c# 以编程方式更改 xaml?

I'm trying to do is hide or replace a specific element, using a if statement within my class file.我正在尝试使用类文件中的 if 语句隐藏或替换特定元素。

The code I'm trying to manipulate is below, to be more specific I want to know how to replace Spin="True" with Spin="False" .我试图操作的代码如下,更具体地说,我想知道如何用Spin="False"替换Spin="True" Spin="False" I'm not sure how it should be done and believe that I need to edit XAML at run-time via code somehow.我不确定应该如何完成,并相信我需要在运行时通过代码以某种方式编辑 XAML。

<fa:ImageAwesome Icon="Refresh" Spin="True" Height="48" Width="48" Margin="0,350,0,0" />

So at the current state spin is equal to true so therefore the icon within the grid would spin, but I would like to set spin is equal to false during some form of if statement or simply within 5 seconds of the current form being active.因此,在当前状态下,spin 等于 true,因此网格内的图标会旋转,但我想在某种形式的 if 语句期间或仅在当前表单处于活动状态的 5 秒内将 spin 设置为 false。

If i understand you correctly you want to control your Spin property programmatically.如果我理解正确,您想以编程方式控制 Spin 属性。

You should use the binding option in WPF , Please look into it more thoroughly since it is the basic principle of WPF and Xaml.您应该使用 WPF 中的 binding 选项,请仔细研究它,因为它是 WPF 和 Xaml 的基本原理。

Here is an example of what i think you wanted to do :这是我认为你想做的一个例子:

Xaml part : Xml 部分:

<fa:ImageAwesome Icon="Refresh" 
      Spin="{Binding SpinProperty}" 
      Height="48" Width="48" Margin="0,350,0,0" />

ViewModel (in case using MVVM ) : ViewModel(在使用 MVVM 的情况下):

private bool m_spinProp;
public MainVM()
{
    m_spinProp = true;
}
public bool SpinProperty
{
    get { return m_spinProp; }
    set { SetProperty(ref m_spinProp, value); }
}

Did you try using the name of the control like?您是否尝试使用控件的名称?

 <fa:ImageAwesome **x:Name="imgSpine"** Icon="Refresh" Spin="True" Height="48" Width="48" Margin="0,350,0,0" />

and just you can check the property of the control.并且您可以检查控件的属性。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM