我遇到的問題主要是因為我Expander裡的ContentTemplate裡的ListBox裡的ItemTemplate裡的DataTemplate裡的TextBox的Text明明綁定感覺寫得很完整了但還是竟然綁不到資料

I have the problem that Expander > ContentTemplate >ListBox>ItemTemplate>DataTemplate>TextBox>Text I think the Binding code I write is really completed,

But When Run It Still shows the binding not work,



而且重點是我把ListBox的XAML碼移出Expander的時候明明就運作得好好的,

Besides if I move the ListBox XAML code out the Expander, the Binding works Smoothly,


所以我覺得應該是放進Expander的時候被屏蔽了之類的,

So I guess maybe the ListBox Binding is concealed,


但是我網路上查察試了很多方法包括直接指定ElementName為Window的xName, 調RelativeSource的AncestorType到Window,亂試了一些TemplateBinding,卻都毫無效果,

But I search the Internet and try many way such as, Assign the ElementName as Window's xName, Set RelatieSource AncestorType to Window, and some other way,

still not work at all,


最後我搜尋這篇文章標題差不多的關鍵字,終於找到一篇文章解決了我的問題,

Finally I use This article's Title as keyword to search the Net And Find an article that solve my problem,


The Link is 文章連結是:

http://stackoverflow.com/questions/14117727/wpf-listbox-w-datatemplate-binding-issue



原來我在Expander後有寫Header="{Binding}" 但沒寫Content="{Binding}"所以ContentTemplate裡的東西無法綁定,寫上後就成功了

這沒遇過真的是不知道原來有這種......設計ORZ

I Find it that in Expander I write the  Header="{Binding}" but I don't write the Content="{Binding}", so things in the ContentTemplate cannot Binding,

When I write the Conrtent Binding behind the Expander, It succesfully Works.



最後附上我的XAML碼

Here is my XAML code

------------------------



<Expander Header="{Binding}" Content="{Binding}"  Grid.Row="1" Style="{StaticResource BtmExpander}" SourceUpdated="OnContentChanged">
                <Expander.HeaderTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="這裡是Expander的標題"/>
                            <CheckBox Grid.Column="1" Style="{StaticResource BtmRightCheckBox}" 
                                         IsChecked="{Binding IsCheck2}"/>
                        </Grid>
                    </DataTemplate>
                </Expander.HeaderTemplate>
                <Expander.ContentTemplate>
                    <DataTemplate>
                        <ListBox  ItemsSource="{Binding ShowList2,Mode=TwoWay}" MaxHeight="100" MinHeight="30">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel>

                                        <TextBox Text ="{  Binding Path =Message,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </DataTemplate>
                </Expander.ContentTemplate>
            </Expander>
arrow
arrow
    全站熱搜

    olivermode 發表在 痞客邦 留言(0) 人氣()