如何从项目视图模态中单独获取项目(一一)

时间:2018-08-05 11:56:20

标签: c# .net wpf xaml data-binding

我如何在项目视图模式中将项目作为字符串获取。 我尝试了以下方法,但是它们没有给我正确的输出。

 if (Opf.ShowDialog() == true)
            {
                StreamWriter swa = new StreamWriter(Opf.FileName);
                using (swa)
                {
                    for (int i = 0; i < PlayList.Items.Count; i++)
                    {
                        var ix = PlayList.Items.GetItemAt(i).ToString();
                        swa.WriteLine(ix);
                    }
                }
                MessageBox.Show("List Saved.");
            }

它给了我

Wss.ItemViewModal

Wss.ItemViewModal

Wss.ItemViewModal

Wss.ItemViewModal

我如何从列表框中获取项目。我的列表框xaml代码

  <ListBox Name="PlayList" Margin="0,50,0,30" Style="{DynamicResource ListBoxStyle1}" Background="Transparent" BorderThickness="0" Foreground="White" ItemsSource="{Binding Items, Mode=TwoWay}" MouseDoubleClick="PlayList_MouseDoubleClick">
            <!--Style="{DynamicResource ListBoxStyle1}" SelectionChanged="PlayList_SelectionChanged"-->
            <ListBox.ItemTemplate >
                <DataTemplate DataType="{x:Type local:ItemViewModel}">
                    <Grid>
                        <Grid.Resources>
                            <Style TargetType="{x:Type Label}">
                                <Setter Property="VerticalAlignment" Value="Center"/>
                            </Style>
                        </Grid.Resources>
                        <Label Content="{Binding Sname}"    FontSize="20"  Foreground="White" x:Name="SongNameList" Margin="0"           HorizontalAlignment="Left"   Width="193"/>
                        <Label Content="{Binding Duration}" FontSize="14" HorizontalContentAlignment="Center" Foreground="Orange" x:Name="DurationList" Margin="189,0,0,0"   HorizontalAlignment="Left"    Width="62"/>
                        <Label Content="{Binding Isvid}"    FontSize="20" HorizontalContentAlignment="Right" Foreground="DeepPink" x:Name="VideoC"       Margin="0,0,300,0"   HorizontalAlignment="Right"   Width="55"/>
                        <Label Content="{Binding Format }"  FontSize="12" HorizontalContentAlignment="Right" Foreground="Orange" x:Name="Format"       Margin="0,0,220,0"   HorizontalAlignment="Right"   Width="50"/>
                        <Label Content="{Binding YTL}"      FontSize="20" HorizontalContentAlignment="Right" Foreground="White" x:Name="YT"           Margin="0,0,100,0"   HorizontalAlignment="Right"   Width="148"/>
                        <Label Content="{Binding SNN}"      FontSize="20" HorizontalContentAlignment="Right" Foreground="SkyBlue" x:Name="SN"           Margin="0"    HorizontalAlignment="Right"   Width="95"/>
                        <Label Content="{Binding VPath }"   FontSize="20" Foreground="Green" x:Name="Path"  Margin="256,0,332,0"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

您只想发表评论。

非常感谢。

2 个答案:

答案 0 :(得分:3)

您可以覆盖 ItemViewModel 类的方法 ToString()

public class ItemViewModel
{
   ...
   public override string ToString()
   {
      return $"ItemViewModel: {Sname} {Duration} {Isvid} {Format } {YTL} {SNN} {VPath}";
   }
   ...
}

答案 1 :(得分:2)

如果未在类中重写,则ToString()返回带有命名空间的类名称,该名称空间将变为Wss.ItemViewModal。出于导出目的而覆盖ToString()并不是最佳解决方案-导出格式对于单个类可能有所不同!在ItemViewModal中实现IFormattable并指定格式更有意义。

或者:不要使用ToString并列出应导出的所有属性:

if (Opf.ShowDialog() == true)
{
    using (StreamWriter swa = new StreamWriter(Opf.FileName))
    {
        foreach(ItemViewModal vm in PlayList.Items)
        {
            var ix = vm.Sname + " " + vm.Duration;
            swa.WriteLine(ix);
        }
    }
    MessageBox.Show("List Saved.");
}