2016-12-19 12 views
0

寻找一点见解。取消选中复选框内的bulletdecorator WPF

我有一个复选框,我已经设置为一种风格,因为我需要使实际的框匹配文本的大小和位置。样式代码如下:

Style TargetType="{x:Type CheckBox}" x:Key="recogCheckbox"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type CheckBox}"> 
       <StackPanel Orientation="Horizontal"> 
        <CheckBox Margin="130,4,0,0"/> 
        <BulletDecorator Name="customBullet" Margin="2,0,0,0" Grid.Column="0" Grid.Row="0" VerticalAlignment="Center"> 
        <TextBlock Width="100" TextWrapping="Wrap" HorizontalAlignment="Left" Foreground="White" Margin="5,0,0,0" Text="Recognition Call"/> 
        </BulletDecorator> 
        <ContentPresenter/> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style>  

的复选框的XAML代码是这样的:

<CheckBox FontFamily="Microsoft Sans Serif" FontSize="12" FontWeight="Bold" Height="20" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="717,193,0,0" 
       Name="recognitionCheckBox" Style="{StaticResource recogCheckbox}" VerticalAlignment="Top" Width="384" > 
</CheckBox> 

我想用我的应用程序明确的按钮,以便取消选中该复选框,但是当我通过使用recognitionCheckBox.isChecked = false引用复选框名称'recognitionCheckBox',它不会取消选中我认为是因为实际框位于我在后面的C#代码中引用时遇到问题的样式中的复选框。

有谁知道一种方法来取消选中样式中的复选框。我一直在寻找几个网站,但没有找到任何帮助我的东西。

回答

0

绑定使用{TemplateBinding}中的ControlTemplate的CheckBox控件本身的器isChecked物业内CheckBox的器isChecked财产:

<Style TargetType="{x:Type CheckBox}" x:Key="recogCheckbox"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type CheckBox}"> 
       <StackPanel Orientation="Horizontal"> 
        <CheckBox IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" Margin="130,4,0,0"/> 
        <BulletDecorator Name="customBullet" Margin="2,0,0,0" Grid.Column="0" Grid.Row="0" VerticalAlignment="Center"> 
         <TextBlock Width="100" TextWrapping="Wrap" HorizontalAlignment="Left" Foreground="White" Margin="5,0,0,0" Text="Recognition Call"/> 
        </BulletDecorator> 
        <ContentPresenter/> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

然后它将反映复选框控件的IsChecked属性的当前值。

+0

我已经尝试了按照您的建议进行绑定。即使尝试手动绑定,而不是模板绑定,但仍无法清除复选框。我可能应该多解释一下。如果我创建的表单上的某个人勾选了复选框,然后决定按下清除表单按钮,它应该清除复选框中的勾号,但它不起作用。 –

+0

将Binding的模式更改为TwoWay,它应该可以工作。我编辑了我的答案。 – mm8

+0

刚刚回到今天工作,并试用它,它完美的作品。将需要记住这一点以供将来使用。非常感谢你。标记为接受的答案。 –

0

从这里摘自:https://msdn.microsoft.com/en-us/library/ms752319(v=vs.85).aspx添加控件模板下面的StackPanel后

<ControlTemplate.Triggers> 
      <Trigger Property="IsChecked" Value="false"> 
      <Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/> 
      </Trigger> 
      <Trigger Property="IsChecked" Value="true"> 
      <Setter TargetName="CheckMark" Property="Visibility" Value="Visible" /> 

     </ControlTemplate.Triggers> 
+0

我没有看到这篇文章,并最终删除了我的代码,因为它只影响特定元素的可见性。我试图使用后面的C#代码来引用样式中的复选框元素,并将isChecked元素设置为false。 –