简体   繁体   English

WPF数据绑定参数问题

[英]Wpf Data binding argument issue

I'm using c# and wpf. 我正在使用c#和wpf。

I have an problem with DataBinding. 我对DataBinding有问题。 I have this model base class: 我有这个模型基类:

public class Media
{
  public string Text {get;set;}
  public List<string> Videos{get;set;}
  public List<string> Images{get;set;}
}

here is my xaml code: 这是我的xaml代码:

<Grid Height="500" Width="380">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <TextBlock Text="{Binding Text, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Center" VerticalAlignment="Center"/>

    <Image Grid.Row="1" Source="{Binding Images[0], Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding Converter={StaticResource imageVisibilityConverter}}"/>

    <MediaElement Grid.Row="1" Source="{Binding Videos[0], Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding Converter={StaticResource videoVisibilityConverter}}"/>
</Grid>

In my Media list view model, some of my models haven't any Videos and Videos is null(or doesn't have any item). 在我的媒体列表视图模型中,我的某些模型没有任何视频,而视频为空(或没有任何项目)。 In binding source of MediaElement I put [0] value of videos that is cause the exception. 在MediaElement的绑定源中,我放入了导致异常的视频的[0]值。

Exception: 例外:

System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'XXXX') from 'Videos' (type 'List`1'). BindingExpression:Path=Videos[0]; DataItem='Media' (HashCode=18855696); target element is 'MediaElement' (Name=''); target property is 'Source' (type 'Uri') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index'

I want to check if videos are available set Videos[0] to MediaElement source property if not, don't set anything to this property. 我想检查视频是否可用,如果不可用,则将Videos [0]设置为MediaElement source属性,不要对此属性设置任何内容。

Any help will be appreciate it. 任何帮助将不胜感激。

Generally speaking if you have to resort to putting logic in things like converters then it's often a good sign that your view model isn't doing it's job properly. 一般来说,如果您不得不在转换器之类的东西中添加逻辑,则通常是一个很好的信号,表明您的视图模型无法正常工作。 This is a good example of that, since you're using models directly and not using view models at all. 这是一个很好的例子,因为您直接使用模型,而根本不使用视图模型。 The binding should fail silently without generating an exception, which indicates to me that it's being generated in your converter. 绑定应该在没有生成异常的情况下静默失败,这向我表明它是在您的转换器中生成的。 If you are going to do logic in your view then I'd probably ditch the converter and use a DataTrigger instead ie something like this: 如果您要在视图中进行逻辑处理,那么我可能会放弃转换器,而使用DataTrigger来代替,例如:

<Image Grid.Row="1">
        <Image.Style>
            <Style TargetType="Image">
                <Setter Property="Visibility" Value="Visible" />
                <Style.Triggers>
                     <!-- Hide when Images is null -->
                    <DataTrigger Binding="{Binding Images}" Value="{x:Null}">
                        <Setter Property="Visibility" Value="Hidden" />
                    </DataTrigger>
                     <!-- Hide when Images[0] is null -->
                    <DataTrigger Binding="{Binding Images[0]}" Value="{x:Null}">
                        <Setter Property="Visibility" Value="Hidden" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Image.Style>
    </Image>

I'm doing it to Visibility here, but you could also use it to attach the Source binding only when it's not null. 我在这里对“可见性”进行操作,但是您也可以使用它仅在不为null时附加Source绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM