0
votes

So I got this windows phone project created with the Caliburn Micro Framework. My goal is to programmatically being able to replace the content in a Grid Row. Or just add a new row in the top of my Grid, so instead of having 2 rows I have 3. The Grid looks like this in .xaml:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <TextBlock Text="{Binding Path=LocalizedResources.NGA, Source={StaticResource LocalizedStrings}}" Visibility="{Binding Path=ShowNoGolferMessage,Mode=TwoWay}"></TextBlock>
    <ListBox Grid.Row="1" x:Name="lstSearch" ItemsSource="{Binding GolferList, Mode=TwoWay}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                //Long list of ListBox items.
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

So my thought was to bind the grid to a variable using x:Name:

<Grid x:Name="golfereGrid">

But I couldn't get that to work. Then I tried setting a Binding to the Grids DataContext:

<Grid DataContext="{Binding golfereGrid, Mode=TwoWay}"> 

It didn't work either in both cases my golfereGrid turned out to be null. My golfereGrid looks like this:

private Grid _golfereGrid;
public Grid golfereGrid
{
    get { return _golfereGrid; }
    set
    {
        _golfereGrid = value;
        NotifyOfPropertyChange(() => golfereGrid);
    }
}

I've been struggling with this for some time now, I would appreciate some help

1
Ok, I've found a semi solution. Create 3 rows in the Grid and then bind the height for row 0 and 1 then when you want row 1 to be on top you just change row 0 height to 0 and row 1 to etc 150. When you need row 0 you give that 150 in height and row 1 0height. Anyone got a better solution? - user2408952

1 Answers

1
votes

If I understand your problem/question you are effectively trying to hide the TextBlock/ListBox depending on if there is content in the ListBox; the easiest way to do this is to use a ValueConverter to convert a bool to Visibility.

For example

public class BooleanToVisibilityConverter : IValueConverter
{
    public bool IsNegation { get; set; }

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (this.IsNegation)
        {
            return (value is bool && (bool)value) ? Visibility.Collapsed : Visibility.Visible;
        }

        return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return value is Visibility && (Visibility)value == Visibility.Visible;
    }
}

In a resource dictionary (that is registered in your App.xaml) define the converters with names that suit you (the following as my example only)

<valueConvertors:BooleanToVisibilityConverter x:Key="BooleanToVisibility" />
<valueConvertors:BooleanToVisibilityConverter x:Key="BooleanToVisibilityReverse" IsNegation="True" />

Update your XAML to use the converter like this:

<Grid>
<Grid.RowDefinitions>
    <RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Path=LocalizedResources.NGA, Source={StaticResource LocalizedStrings}}"
            Visibility="{Binding Path=ShowNoGolferMessage, Converter={StaticResource BooleanToVisibilityReverse}}" />
<ListBox Grid.Row="1" x:Name="lstSearch" ItemsSource="{Binding GolferList, Mode=TwoWay}"
            Visibility="{Binding Path=ShowNoGolferMessage, Converter={StaticResource BooleanToVisibility}}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <!-- Long list of ListBox items -->
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Within your model I'm assuming you have a bool property for ShowNoGolferMessage so when you are getting golfer make suer you call NotifyOfPropertyChange(() => ShowNoGolferMessage) in the setter.

Another suggestion I would make is that you bind to an IEnumerable of some sort - there is a good example here by qmatteo which is pretty useful (his site is full of nuggets!): qmatteoq.com Diary of a Windows Phone developer