c#后面的代码中的访问按钮

时间:2014-10-31 14:00:12

标签: c# xaml

我的xaml代码中有一个按钮" Check"并希望根据正在运行的应用程序的版本将其可见性设置为隐藏或可见。我目前正在尝试通过构造函数中的pragma语句设置它,但它说名称不存在

XAML代码:

<Button Style="{DynamicResource Main.Button}" Name="Check"
    Command="{Binding CheckCommand}" Content="Check Configured Units" Width="150"
    Height="{StaticResource Main.Button.Height}"  />

C#代码背后:

public partial class SinglePlayerSummaryView : UserControl
{
    public SinglePlayerSummaryView()
    {
        InitializeComponent();

#if ReleaseStandard
        Check.Visibility = System.Windows.Visibility.Visible;
#else
        Check.Visibility = System.Windows.Visibility.Hidden;
#endif
    }
}

感谢任何帮助

1 个答案:

答案 0 :(得分:2)

使用x:Name

更改为控件命名的XAML
 <Button x:Name="Check" Style="{DynamicResource Main.Button}" Command="{Binding CheckCommand}" Content="Check Configured Units" Width="150" Height="{StaticResource Main.Button.Height}"  />

然后在你的代码中:

public partial class SinglePlayerSummaryView : UserControl
{
    public SinglePlayerSummaryView()
    {
        InitializeComponent();

       #if ReleaseStandard
            this.Check.Visibility = System.Windows.Visibility.Visible;
       #else
            this. Check.Visibility = System.Windows.Visibility.Hidden;
       #endif
    }
}

它可能有用。