简体   繁体   中英

Wpf Data binding argument issue

I'm using c# and wpf.

I have an problem with 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:

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

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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