WPF checkbox "IsChecked" not reflecting initial binding

Using Microsoft Windows Presentation Foundation and Syncfusion WPF Libraries

WPF checkbox "IsChecked" not reflecting initial binding

Postby norbertjurkiewicz84 on Mon Jun 12, 2017 3:20 pm

I am setting the initial Checkbox value to 1. When I set the DataContext to the binding, I expect the checkbox to reflect the initial binding state. Am I doing something wrong here or is this a bug?


This is a complete self contained example. Just copy and paste into a function. It was tested in 14 (comment out setting OnElementChanged in 14)/15/16 with the same result.

      test;win;binding;⎕USING;str;xml;xaml

⎕USING←'System.IO'
⎕USING,←⊂'System.Windows.Markup'
⎕USING,←⊂'System.Xml,system.xml.dll'
⎕USING,←⊂'System.Windows.Controls,WPF/PresentationFramework.dll'

xaml←'<Window '
xaml,←' xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"'
xaml,←' xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" '
xaml,←' xmlns:d="http://schemas.microsoft.com/expression/blend/2008" '
xaml,←' xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"'
xaml,←' xmlns:local="clr-namespace:testlibrary" '
xaml,←' mc:Ignorable="d" '
xaml,←' Title="Window2" Height="300" Width="300"> '
xaml,←' <Grid > '
xaml,←' <StackPanel DataContext="{Binding MyValues}"> '
xaml,←' <TextBox Text="{Binding TextValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></TextBox> '
xaml,←' <CheckBox Content="test" IsChecked="{Binding CheckBoxValue, Mode=TwoWay}"></CheckBox> '
xaml,←' </StackPanel> '
xaml,←' </Grid> '
xaml,←'</Window>'

str←⎕NEW StringReader(⊂xaml)
xml←⎕NEW XmlTextReader str
win←XamlReader.Load xml

props←⎕NS''
props.MyValues←⎕NS''
props.MyValues.TextValue←'test'
props.MyValues.CheckBoxValue←1
⎕←'Checkbox value should be ticked.'

⎕THIS.⎕FX'ElemChanged (ob ev)' '⎕←ev.Name , '' Value: '', ⍕ob.MyValues ⍎ev.Name '
binding←2015⌶'props'
binding.onElementChanged←(⍕⎕THIS),'.ElemChanged'

win.DataContext←binding
win.Topmost←1
win.Show
User avatar
norbertjurkiewicz84
 
Posts: 62
Joined: Mon Nov 01, 2010 7:26 pm

Re: WPF checkbox "IsChecked" not reflecting initial binding

Postby JohnD|Dyalog on Wed Jun 21, 2017 11:01 am

Hello Norbert,

IsChecked requires a Boolean type, and the default binding will generate an integer from from value 1. You'll need to specify the binding types as the left arg to the ibeam. Something like:

Code: Select all
types←2 2⍴'TextValue'String'CheckBoxValue'Boolean
types←1 2⍴'MyValues'types


Should do it.

/John
User avatar
JohnD|Dyalog
 
Posts: 74
Joined: Wed Oct 01, 2008 9:35 am

Re: WPF checkbox "IsChecked" not reflecting initial binding

Postby norbertjurkiewicz84 on Wed Jun 21, 2017 1:19 pm

JohnD|Dyalog wrote:Hello Norbert,

IsChecked requires a Boolean type, and the default binding will generate an integer from value 1. You'll need to specify the binding types as the left arg to the ibeam. Something like:

Code: Select all
types←2 2⍴'TextValue'String'CheckBoxValue'Boolean
types←1 2⍴'MyValues'types


Should do it.

/John


I figured it was a Boolean to Integer issue and I was unaware that I can nest definitions for complex objects like your exmaple shows.

How would I specify the bindings for a collection item types where an item needs to be a Boolean?


Would something like my example below work? I would think my definition of ItemsCollection would need to specify a Collection type or an array. I may simply be overthinking this...


Code: Select all
TEST;getItem;bindingDef;⎕USING;add;bindingDefItemCol

 ⎕USING←'' 'System'

 getItem←{
     col←⎕NS''
     col.Desc←'Complex Item'
     col.Items←{n←⎕NS'' ⋄ n.CBValue←?1 ⋄ n.TxtValue←⎕D ⋄ n}¨⍳10

     col.Name←⎕NS''
     col.Name.(First Last Middle)←⊂''

     col.Address←⎕NS''
     col.Address.Street←⎕NS''
     col.Address.Street.(Street1 Street2)←⊂''
     col.Address.(City State Zip)←⊂''
     col
 }

 bindingDefItemCol←{

     items←2 2⍴'CBValue'Boolean'TxtValue'String
     items←1 2⍴'Items'items

     name←3 2⍴'First'String'Last'String'Middle'String
     name←1 2⍴'Name'name

     add←1 2⍴'Street'(2 2⍴'Street1'String'Street2'String)
     add⍪←3 2⍴'City'String'State'String'Zip'String
     add←1 2⍴'Address'add

     def←1 2⍴'Desc'String
     def⍪←items
     def⍪←name
     def⍪←add

     def
 }''

 props←⎕NS''
 props.Desc←'My Object Container'
 props.ItemsCollection←getItem¨⍳5

 bindingDef←2 2⍴'Desc'String'ItemsCollection'(↑bindingDefItemCol'')

 binding←bindingDef(2015⌶)'props'

 }

Code: Select all
bindingDef
┌───────────────┬────────────────────────────────────────────┐
│Desc           │(System.String)                             │
├───────────────┼────────────────────────────────────────────┤
│ItemsCollection│┌───────┬──────────────────────────────────┐│
│               ││Desc   │(System.String)                   ││
│               │├───────┼──────────────────────────────────┤│
│               ││Items  │┌────────┬────────────────┐       ││
│               ││       ││CBValue │(System.Boolean)│       ││
│               ││       │├────────┼────────────────┤       ││
│               ││       ││TxtValue│(System.String) │       ││
│               ││       │└────────┴────────────────┘       ││
│               │├───────┼──────────────────────────────────┤│
│               ││Name   │┌──────┬───────────────┐          ││
│               ││       ││First │(System.String)│          ││
│               ││       │├──────┼───────────────┤          ││
│               ││       ││Last  │(System.String)│          ││
│               ││       │├──────┼───────────────┤          ││
│               ││       ││Middle│(System.String)│          ││
│               ││       │└──────┴───────────────┘          ││
│               │├───────┼──────────────────────────────────┤│
│               ││Address│┌──────┬─────────────────────────┐││
│               ││       ││Street│┌───────┬───────────────┐│││
│               ││       ││      ││Street1│(System.String)││││
│               ││       ││      │├───────┼───────────────┤│││
│               ││       ││      ││Street2│(System.String)││││
│               ││       ││      │└───────┴───────────────┘│││
│               ││       │├──────┼─────────────────────────┤││
│               ││       ││City  │(System.String)          │││
│               ││       │├──────┼─────────────────────────┤││
│               ││       ││State │(System.String)          │││
│               ││       │├──────┼─────────────────────────┤││
│               ││       ││Zip   │(System.String)          │││
│               ││       │└──────┴─────────────────────────┘││
│               │└───────┴──────────────────────────────────┘│
└───────────────┴────────────────────────────────────────────┘
User avatar
norbertjurkiewicz84
 
Posts: 62
Joined: Mon Nov 01, 2010 7:26 pm


Return to WPF & Syncfusion

Who is online

Users browsing this forum: No registered users and 1 guest