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



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

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>

任何你想问的问题,请评论。

非常感谢。

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

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

如果在类中未被覆盖,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.");
}

最新更新