复选框
<h3>CheckBox属性用法</h3>
<ol>
<li><strong>x:Name</strong>:
<ul>
<li>为 <code>CheckBox</code> 指定一个名称,以便在代码中引用。
<pre><code class="language-csharp">&lt;CheckBox x:Name=&quot;exampleCheckBox&quot;/&gt;</code></pre></li>
</ul></li>
<li><strong>Content</strong>:
<ul>
<li>设置 <code>CheckBox</code> 的显示文本或内容。
<pre><code class="language-csharp">&lt;CheckBox Content=&quot;Accept Terms and Conditions&quot;/&gt;</code></pre></li>
</ul></li>
<li><strong>IsChecked</strong>:
<ul>
<li>指定 <code>CheckBox</code> 是否被选中。可以是 <code>True</code>、<code>False</code> 或 <code>null</code>(表示不确定状态)。
<pre><code class="language-csharp">&lt;CheckBox IsChecked=&quot;True&quot;/&gt;</code></pre></li>
</ul></li>
<li><strong>IsEnabled</strong>:
<ul>
<li>控制 <code>CheckBox</code> 是否可以被用户编辑。设为 <code>False</code> 时,<code>CheckBox</code> 将变为只读。
<pre><code class="language-csharp">&lt;CheckBox IsEnabled=&quot;False&quot;/&gt;</code></pre></li>
</ul></li>
<li><strong>HorizontalContentAlignment</strong> 和 <strong>VerticalContentAlignment</strong>:
<ul>
<li>设置 <code>CheckBox</code> 内容的水平和垂直对齐方式。
<pre><code class="language-csharp">&lt;CheckBox HorizontalContentAlignment=&quot;Center&quot; VerticalContentAlignment=&quot;Center&quot;/&gt;</code></pre></li>
</ul></li>
<li><strong>ToolTip</strong>:
<ul>
<li>为 <code>CheckBox</code> 添加一个工具提示,当用户将鼠标悬停在控件上时显示。
<pre><code class="language-csharp">&lt;CheckBox ToolTip=&quot;Check this box to accept the terms&quot;/&gt;</code></pre></li>
</ul></li>
<li><strong>模版用例</strong>
<pre><code class="language-csharp">&lt;Window.Resources&gt;
&lt;Style x:Key=&quot;CheckBoxStyle1&quot; TargetType=&quot;{x:Type CheckBox}&quot;&gt;
&lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;1&quot;/&gt;
&lt;Setter Property=&quot;Template&quot;&gt;
&lt;Setter.Value&gt;
&lt;ControlTemplate TargetType=&quot;{x:Type CheckBox}&quot;&gt;
&lt;Grid x:Name=&quot;templateRoot&quot; Background=&quot;Transparent&quot; SnapsToDevicePixels=&quot;True&quot;&gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
&lt;ColumnDefinition Width=&quot;*&quot;/&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;Border Grid.Column=&quot;1&quot; x:Name=&quot;checkBoxBorder&quot; Width=&quot;50&quot; Height=&quot;25&quot; CornerRadius=&quot;13&quot; BorderThickness=&quot;2&quot; Background=&quot;BlueViolet&quot;&gt;
&lt;Grid x:Name=&quot;markGrid&quot;&gt;
&lt;Ellipse Width=&quot;20&quot; Height=&quot;20&quot; Fill=&quot;Aquamarine&quot; HorizontalAlignment=&quot;Left&quot; x:Name=&quot;ccc&quot;&gt;&lt;/Ellipse&gt;
&lt;/Grid&gt;
&lt;/Border&gt;
&lt;ContentPresenter Grid.Column=&quot;0&quot; x:Name=&quot;contentPresenter&quot; HorizontalAlignment=&quot;Right&quot; VerticalAlignment=&quot;Center&quot; Margin=&quot;3&quot;/&gt;
&lt;/Grid&gt;
&lt;ControlTemplate.Triggers&gt;
&lt;Trigger Property=&quot;IsChecked&quot; Value=&quot;true&quot;&gt;
&lt;Setter Property=&quot;HorizontalAlignment&quot; Value=&quot;Right&quot; TargetName=&quot;ccc&quot;&gt;&lt;/Setter&gt;
&lt;/Trigger&gt;
&lt;/ControlTemplate.Triggers&gt;
&lt;/ControlTemplate&gt;
&lt;/Setter.Value&gt;
&lt;/Setter&gt;
&lt;/Style&gt;
&lt;/Window.Resources&gt;
&lt;CheckBox Style=&quot;{DynamicResource CheckBoxStyle1}&quot; Width=&quot;90&quot; FontSize=&quot;15&quot; IsChecked=&quot;{Binding IsSwitchOn, Mode=TwoWay}&quot;&gt;开关&lt;/CheckBox&gt;</code></pre>
<h3>MVVMViewModel</h3></li>
</ol>
<pre><code class="language-csharp">using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace YourNamespace
{
public class MainViewModel : INotifyPropertyChanged
{
private bool _isSwitchOn;
public bool IsSwitchOn
{
get { return _isSwitchOn; }
set
{
_isSwitchOn = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}</code></pre>