【问题标题】:Binding a Grid to variable, using Caliburn Micro使用 Caliburn Micro 将网格绑定到变量
【发布时间】:2014-06-20 07:54:20
【问题描述】:

所以我得到了这个使用 Caliburn 微框架创建的 windows phone 项目。我的目标是以编程方式替换网格行中的内容。或者只是在我的网格顶部添加一个新行,所以我有 3 行而不是 2 行。网格在 .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:Name 将网格绑定到一个变量:

<Grid x:Name="golfereGrid">

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

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

在这两种情况下都不起作用,我的 golfereGrid 结果为空。我的 golfereGrid 看起来像这样:

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

我已经为此苦苦挣扎了一段时间,希望能得到一些帮助

【问题讨论】:

  • 好的,我找到了一个半解决方案。在网格中创建 3 行,然后绑定第 0 行和第 1 行的高度,然后当您希望第 1 行位于顶部时,您只需将第 0 行高度更改为 0,将第 1 行更改为等 150。当您需要第 0 行时,您给出 150在高度和第 1 行 0 高度。谁有更好的解决方案?

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


【解决方案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 属性,所以当你让 Golfer make suer 时,你在 setter 中调用 NotifyOfPropertyChange(() => ShowNoGolferMessage) 。

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

【讨论】:

    猜你喜欢
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    相关资源
    最近更新 更多