使用Caliburn Micro将网格绑定到变量

时间:2014-06-20 07:54:20

标签: c# xaml windows-phone-8 caliburn.micro

所以我用Caliburn Micro Framework创建了这个Windows Phone项目。我的目标是以编程方式替换网格行中的内容。或者只是在我的网格顶部添加一个新行,所以不要有2行我有3.网格在.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>

所以我的想法是使用x将名称绑定到一个变量:名称:

<Grid x:Name="golfereGrid">

但我无法让它发挥作用。然后我尝试将绑定设置为Grids DataContext:

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

在两种情况下,我的golfereGrid结果都是null,但它无法正常工作。我的golfereGrid看起来像这样:

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

我一直在努力解决这个问题,我很感激帮助

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题/问题,你有效地试图隐藏TextBlock / ListBox,具体取决于ListBox中是否有内容;最简单的方法是使用ValueConverter将bool转换为Visibility。

例如

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;
    }
}

在资源字典中(在App.xaml中注册)定义具有适合您名称的转换器(以下仅作为我的示例)

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

更新你的XAML以使用这样的转换器:

<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>

在你的模型中我假设你有一个ShowNoGolferMessage的bool属性,所以当你得到高尔夫球手时,你可以在setter中调用NotifyOfPropertyChange(()=&gt; ShowNoGolferMessage)。

我要做的另一个建议是你绑定到某种类型的IEnumerable - 这里有一个很好的例子,qmatteo非常有用(他的网站充满了金块!):qmatteoq.com Diary of a Windows Phone developer