3

I have a Window with a ListBox which has a DataTemplate, bound to an ObservableCollection of LogItems. The ItemsSource of the ListBox is set in code to the collection; the bindings on the TextBox and TextBlock which make up the DataTemplate are set in XAML. So far, so conventional. However, I need to set the font size/family for the TextBlock at runtime. Currently this information is held in a static cGlobals class. So I need to be able to bind the TextBlock.Text to the LogItems collection, but the TextBlock.FontSize property to the cGlobals.LogFontSize property. How can I do this, either via binding as sketched out in the XAML below, or in code?

       <ListBox   . . .  .  >

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid  HorizontalAlignment="Stretch" . . .  . >

                    <Grid.RowDefinitions>
                        <RowDefinition  Height="20"  />
                        <RowDefinition Height="*" MinHeight="40" />
                    </Grid.RowDefinitions>

                    <TextBox Grid.Row="0"  Background="Honeydew" Text="{Binding  Mode=OneWay, Path=Header,  . . . . />
                    <TextBlock FontSize="{Binding ??????}"  Grid.Row="1" Text="{Binding  Path=BodyText}"  />

                </Grid>
            </DataTemplate >
        </ListBox.ItemTemplate >
    </ListBox>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
peterG
  • 1,651
  • 3
  • 14
  • 23

2 Answers2

1

xaml

<Window x:Class="WpfApplication6.StaticBinding"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication6"

    Title="StaticBinding" Height="300" Width="300">
<Grid>
    <TextBlock FontSize="{Binding Source={x:Static local:Global.FontSize}}" Text="abc"/>
</Grid>

Global

public class Global
{ 
    public static double FontSize
    {
        get { return 20.0; }
    }
}
yo chauhan
  • 12,079
  • 4
  • 39
  • 58
  • This is what I was searching for. One thing that particularly threw me is that while local:Global.FontSize works, exposing a Font object as a property of Global then accessing it as Local:Global.MyFont.Size doesn't . . . – peterG Aug 08 '13 at 13:56
0

You will need to declare a public property of Type cGlobals but the class cannot be static because you will need to use it as a return type. It does not look like you are following the Model-View-ViewModel pattern, since you are assigning the ItemsSource in the code-behind instead of XAML, so you will need to declare the property in the code-behind. In your Code-Behind(your .xaml.cs file)

private CGlobals _cGlobals;

public CGlobals CGlobals{get{return _cGlobals;}}

public CodeBehindConstructor(){
    _cGlobals = new CGlobal{FontSize = 12, FontFamily="Times New Roman"};

}



xaml:

<Window  Name="TheWindow">

 <TextBlock FontSize="{Binding CGlobals.FontSize, ElementName=TheWindow}"  Grid.Row="1" Text="{Binding  Path=BodyText}"  />


</Window>