简体   繁体   English

WPF XAML样式复选框模板

[英]wpf xaml checkbox template in style

first post so please go easy. 第一篇文章,所以请放轻松。 I am trying to get the value of a checkbox from another page (this is part of a navigation form in WPF project), but it always resets the value to false as soon as the page is changed. 我试图从另一个页面获取复选框的值(这是WPF项目中导航表单的一部分),但是一旦页面更改,它总是将值重置为false。 How do I get the IsChecked value to bind properly? 如何获取IsChecked值以正确绑定? I am learning just starting to work with WPF so the template is very basic, if there is a better way to do this then I am open to it. 我正在学习刚刚开始使用WPF,因此模板非常基础,如果有更好的方法可以做到这一点,那么我可以接受。

Also the code behind page is vb.net not c# if that is going to play a part. 另外,页面后面的代码是vb.net而不是c#(如果这将发挥作用)。

the style is called as follows: 样式如下:

<CheckBox Content="Photo" x:Name="OT_Photo" Grid.Row="0" Grid.Column="0" Style="{StaticResource ElementCheckbox}" />

There are several hundred checkboxes across these pages so I would very much like to avoid variables for each one. 这些页面上有数百个复选框,因此我非常想避免每个页面都有变量。

The style is in 'Application.xaml' 样式在“ Application.xaml”中

<Style TargetType="CheckBox" x:Key="ElementCheckbox">
        <Setter Property="IsThreeState" Value="True" />
        <Setter Property="IsChecked" Value="{Binding IsChecked ,Mode=TwoWay}"  /> 
        <Setter Property="Foreground" Value="Navy" />
        <Setter Property="FontSize" Value="14" />
        <Setter Property="Margin" Value="15,4,0,4" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="MaxWidth" Value="500" />
        <Setter Property="Template">
              <Setter.Value>
                <ControlTemplate TargetType="{x:Type CheckBox}">
                    <DockPanel DockPanel.Dock="Left">
                        <Border x:Name="Border" CornerRadius="0" VerticalAlignment="Top">
                            <Image Height="20" Width="20" Name="CheckMark" />
                        </Border>
                        <TextBlock x:Name="CheckText" Text="{TemplateBinding Content}" Margin="5,0,0,0" FontSize="{TemplateBinding FontSize}" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" />
                    </DockPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="{x:Null}">
                            <Setter TargetName="CheckMark" Property="Source" Value="CheckBoxNull.png" />
                        </Trigger>
                        <Trigger Property="IsChecked" Value="false">
                            <Setter TargetName="CheckMark" Property="Source" Value="CheckBoxCross.png" />
                        </Trigger>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="CheckMark" Property="Source" Value="CheckBoxTick.png" />
                        </Trigger>
                         <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Effect" TargetName="Border">
                                <Setter.Value>
                                    <DropShadowEffect ShadowDepth="1" Color="Blue" Opacity="1" BlurRadius="2"/>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Blue" TargetName="CheckText" />
                        </Trigger>
                          </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Thanks in advance of any help! 在此先感谢您的帮助!

I should add I have been stuck on this for over a day, I have tried template binding, binding paths, and an on click procedure based on the c# answers I have found already. 我应该补充一点,我已经坚持了一天多,我已经尝试过模板绑定,绑定路径以及基于已经发现的C#答案的单击过程。 always resulting in the checkbox being reset though. 总是导致该复选框被重置。

Solved 解决了

My problem was unrelated to the checkbox as it happened, the page was not named statically so the properties always served the default values. 我的问题与复选框无关,因为该页面不是静态命名的,因此属性始终提供默认值。 (rookie mistake - much shame) (菜鸟错误-很可惜)

on the up side the code above works perfectly (in its limited way) if anyone wants to pinch it! 从正面看,如果有人想要捏住上面的代码,它可以完美地工作(以其有限的方式)!

VB.NET: VB.NET:

Private Sub MainForm_Loaded(sender As Object, e As RoutedEventArgs) Handles MainForm.Loaded
    Me.NavigationService.Navigate(ContactDetailsPage)
End Sub

instead of XAML stating the source: 而不是XAML说明来源:

<NavigationWindow x:Name="MainForm" x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="" Source="ContactDetails.xaml">

I think the problem is that you define your binding in your template instead in your control. 我认为问题在于您在模板中而不是在控件中定义绑定。 You usually don't do bindings in your templates unless it is a DataTemplate. 除非它是DataTemplate,否则通常不会在模板中进行绑定。

<CheckBox IsChecked="{Binding Value}" Content="Photo" x:Name="OT_Photo" Grid.Row="0" Grid.Column="0" Style="{StaticResource ElementCheckbox}" />

Don't forget to set de DataContext for your object. 不要忘记为您的对象设置de DataContext。

You might want to test the binding first without templating. 您可能需要先测试绑定而不进行模板测试。 If this doesn't solve your problem it might be useful to post the code-behind as well. 如果这不能解决您的问题,则将代码隐藏在后面也可能很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM