我必須在同一行中顯示10個相同的用戶控件。每個控件將在左側包含產品圖像,右側將包含產品的名字,姓氏和類別。控件必須與它們之間的適當空間以及它們中的文本對齊,並且必須是可讀的。我在GridView行中使用了一個堆棧面板,每個控件給出窗口總寬度的10%。在我的代碼中的問題是,控件和控件中的文本之間沒有空格不可讀。60英寸屏幕上的多個控件的WPF屏幕顯示
<StackPanel Orientation="Horizontal" Grid.Row="3" VerticalAlignment="Bottom" Margin="0,0,5,0" >
<my:ScannedItemUserControl x:Name="ScannedProduct12" Margin="0,8,0,8"
Width="{Binding
Converter={StaticResource PercentageConverter},
ElementName=MyWindow,
Path=ActualWidth,
ConverterParameter=0.1}"
FontSize="{Binding
Converter={StaticResource PercentageConverter},
ElementName=ScannedProduct12,
Path=ActualWidth,
ConverterParameter=0.06}"
/>
<my:ScannedItemUserControl x:Name="ScannedProduct11" Margin="0,8,0,8" Width="{Binding
Converter={StaticResource PercentageConverter},
ElementName=MyWindow,
Path=ActualWidth,
ConverterParameter=0.1}"
FontSize="{Binding
Converter={StaticResource PercentageConverter},
ElementName=ScannedProduct11,
Path=ActualWidth,
ConverterParameter=0.06}"/>
<my:ScannedItemUserControl x:Name="ScannedProduct10" Margin="0,8,0,8" HorizontalAlignment="Right" Width="{Binding
Converter={StaticResource PercentageConverter},
ElementName=MyWindow,
Path=ActualWidth,
ConverterParameter=0.1}"
FontSize="{Binding
Converter={StaticResource PercentageConverter},
ElementName=ScannedProduct10,
Path=ActualWidth,
ConverterParameter=0.06}"/>
<my:ScannedItemUserControl x:Name="ScannedProduct9" Margin="0,8,0,8" Width="{Binding
Converter={StaticResource PercentageConverter},
ElementName=MyWindow,
Path=ActualWidth,
ConverterParameter=0.1}"
FontSize="{Binding
Converter={StaticResource PercentageConverter},
ElementName=ScannedProduct9,
Path=ActualWidth,
ConverterParameter=0.06}"/>
<my:ScannedItemUserControl x:Name="ScannedProduct8" Margin="0,8,0,8" Width="{Binding
Converter={StaticResource PercentageConverter},
ElementName=MyWindow,
Path=ActualWidth,
ConverterParameter=0.1}"
FontSize="{Binding
Converter={StaticResource PercentageConverter},
ElementName=ScannedProduct8,
Path=ActualWidth,
ConverterParameter=0.06}"/>
<my:ScannedItemUserControl x:Name="ScannedProduct7" Margin="0,8,0,8" Width="{Binding
Converter={StaticResource PercentageConverter},
ElementName=MyWindow,
Path=ActualWidth,
ConverterParameter=0.1}"
FontSize="{Binding
Converter={StaticResource PercentageConverter},
ElementName=ScannedProduct7,
Path=ActualWidth,
ConverterParameter=0.06}"/>
<my:ScannedItemUserControl x:Name="ScannedProduct6" Margin="0,8,0,8" Width="{Binding
Converter={StaticResource PercentageConverter},
ElementName=MyWindow,
Path=ActualWidth,
ConverterParameter=0.1}"
FontSize="{Binding
Converter={StaticResource PercentageConverter},
ElementName=ScannedProduct6,
Path=ActualWidth,
ConverterParameter=0.06}"/>
<my:ScannedItemUserControl x:Name="ScannedProduct5" Margin="0,8,0,8" HorizontalAlignment="Right" Width="{Binding
Converter={StaticResource PercentageConverter},
ElementName=MyWindow,
Path=ActualWidth,
ConverterParameter=0.1}"
FontSize="{Binding
Converter={StaticResource PercentageConverter},
ElementName=ScannedProduct5,
Path=ActualWidth,
ConverterParameter=0.06}"/>
<my:ScannedItemUserControl x:Name="ScannedProduct4" Margin="0,8,0,8" HorizontalAlignment="Right" Width="{Binding
Converter={StaticResource PercentageConverter},
ElementName=MyWindow,
Path=ActualWidth,
ConverterParameter=0.1}"
FontSize="{Binding
Converter={StaticResource PercentageConverter},
ElementName=ScannedProduct4,
Path=ActualWidth,
ConverterParameter=0.06}"/>
<my:ScannedItemUserControl x:Name="ScannedProduct3" Margin="0,8,0,8" HorizontalAlignment="Right" Width="{Binding
Converter={StaticResource PercentageConverter},
ElementName=MyWindow,
Path=ActualWidth,
ConverterParameter=0.1}"
FontSize="{Binding
Converter={StaticResource PercentageConverter},
ElementName=ScannedProduct3,
Path=ActualWidth,
ConverterParameter=0.06}"/>
</StackPanel>
public class PercentageConverter : IValueConverter
{
public object Convert(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
return System.Convert.ToDouble(value) *
System.Convert.ToDouble(parameter);
}
public object ConvertBack(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
這裏添加一些代碼,讓我們調查,這在我看來就像你需要你的'Grid'中添加容器和可能使用'ControlTemplate'添加統一的外觀,應用風格,它會給你完全控制你想如何顯示內容,觸發器會給你選擇添加一些效果。 – XAMlMAX