WPF's TreeView shows a workaround when item is not stretch

Source: Internet
Author: User

Recently in helping a friend with WPF to achieve a function of the treeviewitem is always the shortest default problem (for my feature is a problem), and found it difficult to find a solution on the Internet (it is difficult to find the style on the Internet when you have to try, especially at the beginning of time, I really do not understand the style code of WPF ..., really with Stones's bright, this place suggests or the state of a friend to use blend, will help, that is, blend sometimes installed may be directly unable to use, My side seems to be a development package of blend deleted after the inexplicable can be used). But writing code is sometimes a matter of light, just as I pale Google and the other English-language web site also can't find the answer, the eyes sweep to a word in the code: Auto.

The default is the shortest, is not the width of the TextBlock is adaptive? At this time I felt as if I had found the crux of the problem, and began to look at the code (at first it just swept the eyes, I do not know where auto is ...). ), irrelevant items are removed after the following:

 <Stylex:key= "Treeviewitemstyle"TargetType="{x:type TreeViewItem}">            <Setter Property= "Template">                <Setter.value>                    <ControlTemplateTargetType="{x:type TreeViewItem}">                        <Grid>                            <grid.columndefinitions>                                <ColumnDefinitionMinWidth= "+"Width= "Auto"/>                                <ColumnDefinitionWidth= "Auto"/>                                <ColumnDefinitionWidth="*"/>                            </grid.columndefinitions>                            <grid.rowdefinitions>                                <RowDefinitionHeight= "Auto"/>                                <RowDefinition/>                            </grid.rowdefinitions>                            <ToggleButtonx:name= "Expander"Clickmode= "Press"IsChecked="{TemplateBinding isexpanded}"Style="{StaticResource Expandcollapsetogglestyle}"isenabled= "False"Visibility= "Collapsed"D:ishidden= "True"horizontalcontentalignment= "Stretch"/>                            <Borderx:name= "Bd"BorderBrush="{TemplateBinding BorderBrush}"borderthickness="{TemplateBinding BorderThickness}"Background="{TemplateBinding Background}"Grid.column= "1"Padding="{TemplateBinding Padding}"Snapstodevicepixels= "true">                                <ContentPresenterx:name= "Part_header"ContentSource= "Header"HorizontalAlignment="{TemplateBinding Horizontalcontentalignment}"Snapstodevicepixels="{TemplateBinding Snapstodevicepixels}"/>                            </Border>                            <Itemspresenterx:name= "ItemsHost"Grid.columnspan= "2"Grid.column= "1"Grid.Row= "1"/>                        </Grid>                    </ControlTemplate>                </Setter.value>            </Setter>        </Style>

The reason for creating this style is to have the TreeView expand by default, but the above code snippet is automatically generated, and I have not made any changes to the style passed to the TreeView's Itemcontainerstyle. You can see that the default grid is divided into three columns, the first column is the TreeView's Collapse button, the second column is the location ContentPresenter our hierarchicaldatatemplate, and it's Auto ...

After discovering this, I took this second column out, and after Itemhosts's columspan=2 was removed, I found that the TreeView was full screen and the problem was solved.

I hope to have a little help with friends who don't know much about WPF, sleep ...

WPF's TreeView shows a workaround when item is not stretch

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.