简体   繁体   中英

Setting Default Value of TextBlock when binding to Boolean Property

I am binding a textblock to a boolean property. What I am trying to do - if it is false I want the value to be In Progress If it is true I want it to be Completed

I am using C# with a WPF application and the textblocks are part of a treeview.

<DataTemplate>
 <Grid>
   <Grid.ColumnDefinitions>
     <ColumnDefinition></ColumnDefinition>
     <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
     <TextBlock Grid.Column="0" FontFamily="Sagoe UI" FontSize="14" Text="{Binding Path=BaseNumber, StringFormat='Device Number {0}.'}" Padding="2" Width="200" />
     <TextBlock Grid.Column="1" FontFamily="Sagoe UI" FontSize="14" Text="{Binding Path=BaseMode}" Padding="2" />
 </Grid>
</DataTemplate>

The TextBlock that is bound to BaseMode is the one I want to set the values for.

Thanks!

Don't bind the Text but set up DataTriggers in the Style , which then in turn set the Text you want for true/false. (Also watch out for precedence to not override the style)

What you can use is a ValueConverter. See if the following tutorials help you:

http://wpftutorial.net/ValueConverters.html

http://drwpf.com/blog/category/value-converters/

 <TextBlock Grid.Column="1" FontFamily="Sagoe UI" FontSize="14" Padding="2" >
            <TextBlock.Style>
                <Style TargetType="TextBlock" >
                    <Setter Property="Text" Value="In Progress"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding BaseMode}" Value="true">
                           <Setter Property="Text" Value="Completed"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
  </TextBlock>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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