1

I am using <Run Text={Binding ...}/> inside a TextBlock in order to concatenate 2 strings (one which is hard coded, the other one is a binding expression on a property). This is being done in a DataTemplate :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <DataTemplate x:Key="MarketOrderDataTemplate">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="30*"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="70*"/>
            </Grid.ColumnDefinitions>

            <!-- Item icon TODO: Add Icon-->
            <Image Grid.Column="0" Margin="10,0,10,0"  Stretch="Fill" Source="../../Images/thumb_icon-mineral-protoss.png" />

            <Separator Grid.Column="1" Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />

            <!-- Item information -->
            <Grid Grid.Column="2" Margin="10,0,10,0" HorizontalAlignment="Center">
                <Grid.RowDefinitions>
                    <RowDefinition Height="33*"/>
                    <RowDefinition Height="33*"/>
                    <RowDefinition Height="33*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Row="0">
                    <Run Text="Item Name : "/>
                    <Run Text="{Binding Item.ItemName}"/>
                </TextBlock>
                <TextBlock Grid.Row="1">
                    <Run Text="Item ID : "/>
                    <Run Text="{Binding Item.ItemID}"/>
                </TextBlock>
                <TextBlock Grid.Row="2">
                    <Run Text="Item Type : "/>
                    <Run Text="{Binding Item.ItemType}"/>
                </TextBlock>
            </Grid>

            <Separator Grid.Column="3" Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />

            <!-- Market order information -->
            <Grid Grid.Column="4" Margin="10,0,10,0" HorizontalAlignment="Center">
                <Grid.RowDefinitions>
                    <RowDefinition Height="33*"/>
                    <RowDefinition Height="33*"/>
                    <RowDefinition Height="33*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Row="0">
                    <Run Text="Market Order ID : "/>
                    <Run Text="{Binding OrderID}"/>
                </TextBlock>
                <TextBlock Grid.Row="1">
                    <Run Text="Price : "/>
                    <Run Text="{Binding Price}"/>
                </TextBlock>
                <TextBlock Grid.Row="2">
                    <Run Text="Volume Remaining : "/>
                    <Run Text="{Binding VolumeRemaining}"/>
                </TextBlock>
            </Grid>
        </Grid>
    </DataTemplate>

</ResourceDictionary>

This works in the Item Information part of my DataTemplate (with bindings on Item.ItemName, Item.ItemID, Item.ItemType) without any problems. When I try to do the same with the Market Order Information part of my DataTemplate (with bindings on OrderID, Price, VolumeRemaining) though, it makes the application crash for some reason.

It works well if I just set the Textproperty on my TextBlock with the same binding expression, but I do not know why it makes the application crash with the <Run Text={Binding ...}/> syntax on the Market Order Info part?

1 Answer 1

4

You did not show your view model or the error so I can only suspect that it's because your property is read-only and Run by default bind both ways. Change binding to OneWay

<Run Text="{Binding VolumeRemaining, Mode=OneWay}"/>
Sign up to request clarification or add additional context in comments.

3 Comments

This worked, but I do not have a readonly property in my viewmodel. OrderID is a uint, Price is a float and VolumeRemaining is a uint.
Type does not matter. What matters is do these properties have public setter as well as getter? In order for property to be bound both ways it must have public getter and setter
They did not have setters, that's why! Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.