A read-only CheckBox in C# WPF

后端 未结 11 2031
南方客
南方客 2020-12-06 04:37

I am having a tricky problem, I want some slightly unusual behaviour from a checkbox and can\'t seem to figure it out. Any suggestions would be most welcome. The behaviour I

相关标签:
11条回答
  • 2020-12-06 05:18

    I've been trying to create my generic ReadOnlyCheckBox style/template but I'm having a problem with the binding to the data. In the example you bind directly to the data from the ControlTemplate definition, but of course this is not really what I want, as I want to be able to declare the new checkbox something like this:

            <CheckBox x:Name="uiComboBox" Content="Does not set the backing property, but responds to it."
    Style="{StaticResource ReadOnlyCheckBoxStyle}" IsChecked="{Binding MyBoolean}"  Click="uiComboBox_Click"/>
    

    Except of course when I do this and then set the event trigger on the bullet to be a TemplateBinding of IsChecked I have exactly what I started with! I guess I don't understand why setting the binding directly in the bullet is different from setting IsChecked and then binding to that, isn't the TemplateBinding just a way of referencing what is set in the properties of the control being created? How is the Click triggering the UI update even tho the data does not get updated? Is there a trigger for Click I can override to stop the update?

    I got all the DictionaryResource stuff working fine so I am happy with that, cheers for the pointer.

    The other thing I was curious about was if it is possible to reduce my Control/Style template by using the BasedOn parameter in the style, then I would only override the things I actually need to change rather than declaring a lot of stuff that I think is part of the standard template anyway. I might have a play with this.

    Cheers

    ed

    0 讨论(0)
  • 2020-12-06 05:19

    Use Validation to block the boolean from getting toggled when you don't want it to - http://www.codeproject.com/KB/WPF/wpfvalidation.aspx

    This is much less scary than the other answer, or hooking Clicked

    0 讨论(0)
  • 2020-12-06 05:23

    This is the class I've written to do something similar, for similar reasons (still raises all the Click and Command events as normal, but does not alter the binding source by default and does not auto-toggle. Unfortunately it does still have the animated fade-in-out on click, which is a bit strange if the click-handling code doesn't end up changing IsChecked.

    public class OneWayCheckBox : CheckBox
    {
        private class CancelTwoWayMetadata : FrameworkPropertyMetadata
        {
            protected override void Merge(PropertyMetadata baseMetadata,
                                          DependencyProperty dp)
            {
                base.Merge(baseMetadata, dp);
    
                BindsTwoWayByDefault = false;
            }
        }
    
        static OneWayCheckBox()
        {
            // Remove BindsTwoWayByDefault
            IsCheckedProperty.OverrideMetadata(typeof(OneWayCheckBox),
                                               new CancelTwoWayMetadata());
        }
    
        protected override void OnToggle()
        {
            // Do nothing.
        }
    }
    

    Usage:

    <yourns:OneWayCheckBox IsChecked="{Binding SomeValue}"
                           Command="{x:Static yourns:YourApp.YourCommand}"
                           Content="Click me!" />
    

    (Note that the IsChecked binding is now one-way by default; you can declare it as TwoWay if you want, but that would defeat part of the point.)

    0 讨论(0)
  • 2020-12-06 05:25

    Wrap the CheckBox in a ContentControl. Make the ContentControl IsEnabled=False

    0 讨论(0)
  • 2020-12-06 05:30

    What about data binding to the IsHitTestVisible property?

    For example, assuming an MVVM approach:

    1. Add a IsReadOnly property to your view model, initially set as true to allow click.
    2. Binding this property to CheckBox.IsHitTestVisible.
    3. After the first click, update your view model to set this value to false, preventing any further clicks.

    I don't have this exact requirement, I just needed an always read only checkbox, and it seems to solve the problem nicely. Also note Goran's comment below about the Focusable property.

    0 讨论(0)
提交回复
热议问题