使用连接字符串编辑champ xaml

时间:2016-06-22 01:06:34

标签: c# wpf xaml loops concatenation

我在我的xaml文件中有6个类似的值:

<TextBlock Name="Player0" Visibility="Hidden" Text="Player 0" />
<TextBlock Name="Player1" Visibility="Hidden" Text="Player 1" />
...

但是我想用这样的for循环改变Visibilty:

for (int i = 0; i<6; i++)
{
  String test = "Player" + i;
  test.Visibility = Visibility.Visible;
}

但它没有用。

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

for (int i = 0; i < 6; i++)
{
  TextBlock test = (TextBlock)this.FindName("Player" + i);
  test.Visibility = Visibility.Visible;
}

希望这有帮助!

答案 1 :(得分:0)

xaml文件中的对象类型是TextBlock,但它在cs文件中是String。 你确定吗?  如果是,请在cs文件中更改类类型,如

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">

<dependency>
  <dependentAssembly>
    <assemblyIdentity
      type="win32"
      name="Microsoft.Windows.Common-Controls"
      version="6.0.0.0" processorArchitecture="*"
      publicKeyToken="6595b64144ccf1df"
      language="*">
    </assemblyIdentity>
  </dependentAssembly>
</dependency>

<dependency>
  <dependentAssembly>
    <assemblyIdentity
      type="win32"
      name="Microsoft.VC90.CRT"
      version="9.0.21022.8"
      processorArchitecture="amd64"
      publicKeyToken="1fc8b3b9a1e18e3b">
    </assemblyIdentity>
  </dependentAssembly>
</dependency>

<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
  <security>
    <requestedPrivileges>
      <requestedExecutionLevel
        level="asInvoker"
        uiAccess="false"/>
    </requestedPrivileges>
  </security>
</trustInfo>

<asmv3:application>
  <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
    <ms_windowsSettings:dpiAware xmlns:ms_windowsSettings="http://schemas.microsoft.com/SMI/2005/WindowsSettings">false</ms_windowsSettings:dpiAware>
  </asmv3:windowsSettings>
</asmv3:application>
<file name="debuggerproxy.dll">
    <comClass clsid="{C5621364-87CC-4731-8947-929CAE75323E}" threadingModel="Both"/>
</file>
  <comInterfaceExternalProxyStub name="CausalityInternal_IAD7ALCausalityEventBridge" iid="{F6A124D7-5BB7-47B2-A9AF-AAB0EEAB60E3}" numMethods="5" proxyStubClsid32="{C5621364-87CC-4731-8947-929CAE75323E}"/>
  <comInterfaceExternalProxyStub name="CausalityInternal_ISDMCausalityEventCallback" iid="{74D108F1-1FD2-4699-991E-C4BAD52E773D}" numMethods="4" proxyStubClsid32="{C5621364-87CC-4731-8947-929CAE75323E}"/>
... many more entries ...
</assembly>