我有2个按钮,开始并捕获。我希望在表单加载时禁用捕获按钮并启用启动。然后单击开始禁用启动按钮并启用捕获。 请帮我。 提前致谢
修改
<Grid>
<Button Content="Button" Height="77" HorizontalAlignment="Left" Margin="92,151,0,0" Name="button1" VerticalAlignment="Top" Width="109">
<Button.Background>
<ImageBrush ImageSource="/WpfApplication1;component/Images/images.jpg" />
</Button.Background>
</Button>
<Button Content="Button" Height="77" HorizontalAlignment="Left" Margin="229,151,0,0" Name="button2" VerticalAlignment="Top" Width="112" IsEnabled="False" >
<Button.Background>
<ImageBrush ImageSource="/WpfApplication1;component/Images/images.jpg" />
</Button.Background>
</Button>
</Grid>
答案 0 :(得分:11)
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Name="BStart" Content="Start" Width="100" Height="50" HorizontalAlignment="Left" Click="BStart_Click" />
<Button Name="BCapture" Content="Capture" Width="100" Height="50" HorizontalAlignment="Right" IsEnabled="False" />
</Grid>
</Window>
您可以通过xaml禁用Capture,然后通过在c#中编写代码来启用它。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void BStart_Click(object sender, RoutedEventArgs e)
{
BStart.IsEnabled = false;
BCapture.IsEnabled = true;
}
}
}
启用和禁用使用IsEnabled属性。并单击按钮使用Click事件。
答案 1 :(得分:1)
设置IsEnabled =&#34; False&#34;禁用按钮
<Button Name="btn" IsEnabled="False" Content="press"/>
设置IsEnabled =&#34; True&#34;启用按钮
<Button Name="btn" IsEnabled="True" Content="press"/>
希望这有帮助
答案 2 :(得分:0)
您应该将当前状态存储在某个变量中(例如_capturing
)。变量更改时,刷新IsEnabled
属性。
Xaml代码:
<Button Content="Start" Name="StartButton" Grid.Row="0" Click="StartButton_Click" />
<Button Content="Capture" Name="CaptureButton" Grid.Row="1" Click="CaptureButton_Click"/>
C#代码:
public partial class MainWindow : Window
{
private bool _capturing;
public MainWindow()
{
InitializeComponent();
// Some code
_capturing = false;
UpdateButtons();
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
// Some code
_capturing = true;
UpdateButtons();
}
private void CaptureButton_Click(object sender, RoutedEventArgs e)
{
// Some code
UpdateButtons();
}
private void UpdateButtons()
{
StartButton.IsEnabled = !_capturing;
CaptureButton.IsEnabled = _capturing;
}
}
<强>更新强>
您应该添加点击处理程序,您的xaml代码将起作用:
<Button Click="StartButton_Click" Content="Button" Height="77" HorizontalAlignment="Left" Margin="92,151,0,0" Name="button1" VerticalAlignment="Top" Width="109">
<Button.Background>
<ImageBrush ImageSource="/WpfApplication1;component/Images/images.jpg" />
</Button.Background>
</Button>
<Button Click="CaptureButton_Click" Content="Button" Height="77" HorizontalAlignment="Left" Margin="229,151,0,0" Name="button2" VerticalAlignment="Top" Width="112" IsEnabled="False" >
<Button.Background>
<ImageBrush ImageSource="/WpfApplication1;component/Images/images.jpg" />
</Button.Background>
</Button>
答案 3 :(得分:0)
是的,我得到了答案。只需简单的解决方案
btnhide.visibility= system.visibility. hidden;
答案 4 :(得分:0)
有两种方法可以在C#WPF中禁用按钮。
.xaml
文件中IsEnabled="False"
。如果将IsEnabled
属性设置为False,则默认情况下将禁用按钮。.cs
文件中ButtonName.IsEnabled = false;
如果将按钮的IsEnabled
属性设置为false,则会禁用按钮