如何添加滚动条,让我滚动水平显示的项目?
<ItemsControl ItemsSource="{Binding Data, ElementName=myWindows}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal">
</VirtualizingStackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="FrameworkElement" >
<Setter Property="Margin" Value="10,0,10,0"></Setter>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud) 我有5个图像所有相同的像素高度和像素宽度(2481*3508).但是,一个是gif,一个jpeg,一个png和一个bmp.现在我将它们渲染成BitmapSource,其中(1)DecodePixelHeight的原始像素高度的三分之二和(2)DecodePixelHeight的原始像素高度.
第一种情况:
bitmapImage.BeginInit();
bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.DecodePixelHeight = 2/3 * originalHeight;
bitmapImage.StreamSource = streamWithTheFile;
bitmapImage.EndInit();
bitmapImage.Freeze();
Run Code Online (Sandbox Code Playgroud)
BMP和Jpeg同样缓慢.Png和Gif只需要不到一半的时间.为什么?
第二种情况:
bitmapImage.BeginInit();
bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = streamWithTheFile;
bitmapImage.EndInit();
bitmapImage.Freeze();
Run Code Online (Sandbox Code Playgroud)
Png之前所需时间的一半.Jpeg和BMP之前需要的时间是第5次.Gif和以前一样.
根据文档,我会假设Png和Jpeg性能在某种程度上比其他格式更独立于实际解码大小.可能是什么原因,它不是?
为了更好地理解WPF绑定:
<Style x:Key="myButton" TargetType="Button">
<Setter
Property="Content"
Value="{Binding
RelativeSource={RelativeSource FindAncestor,AncestorType=My:Control},
Path=Text}">
</Setter>
</Style>
<Button Name="button1" Style="{StaticResource myButton}"></Button>
<Button Name="button2" Style="{StaticResource myButton}"></Button>
Run Code Online (Sandbox Code Playgroud)
当我在多个按钮上使用这个Style时,我认为Style只是实例化的.绑定意味着什么?我只有一个绑定(即一个绑定对象),而button1和button2是否引用了这个绑定对象?如果是这样,当button1和button2被用作不同的My:Control控件的一部分时,何时以及如何确定绑定的来源?我的意思是指源对象而不是源的值?有人能指出一些规范吗?
我想修改System.Drawing.Printing.PrinterSettings对象,我从System.Windows.Forms.PrintDialog得到对话框已显示给用户之后.虽然我可以在PrinterSettings对象上更改属性值,但在打印文档时实际上会考虑在显示对话框后我所做的任何更改.
这是我的意思的一个例子:
//Show the printdialog and retreive the printersettings
var printDialog = new PrintDialog();
if (printDialog.ShowDialog() != DialogResult.OK)
return;
var printerSettings = printDialog.PrinterSettings;
//Now modify the printersettings object
printerSettings.ToPage = 8;
Run Code Online (Sandbox Code Playgroud)
现在使用printerSettings对象进行打印.我使用第三方dll Aspose.Words,因为我需要打印Word,但这似乎不是问题.似乎在显示对话框后,所有设置都已提交到打印机并且更改PrinterSettings什么也没做.有关如何使其工作的任何想法?
编辑:我有一些解决方法.我想要的是得到这些具体问题的答案:是否可以在显示对话框后更改PrinterSettings对象,并在打印时考虑这些更改.如果有人知道只有一个如何这样的工作方式(你可以在你想用于打印什么API决定,它不只要PrinterSettings对象使用关系),我会非常感激.
当我将ListBox的ItemsSource绑定到List时,绑定引擎在控件消失后保持列表元素.这会导致所有列表元素保留在内存中.使用ObservalbleCollection时问题就消失了.为什么会这样?
窗口标记内的xaml
<Grid>
<StackPanel>
<ContentControl Name="ContentControl">
<ListBox ItemsSource="{Binding List, Mode=TwoWay}" DisplayMemberPath="Name"/>
</ContentControl>
<Button Click="Button_Click">GC</Button>
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
代码背后:
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.DataContext = null;
ContentControl.Content = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
Run Code Online (Sandbox Code Playgroud)
视图模型
class ViewModel : INotifyPropertyChanged
{
//Implementation of INotifyPropertyChanged ...
//Introducing ObservableCollection as type resolves the problem
private IEnumerable<Person> _list =
new List<Person> { new Person { Name = "one" }, new Person { Name = "two" } …Run Code Online (Sandbox Code Playgroud) 我有以下Multibinding:
<Grid.Visibility>
<MultiBinding Converter="{StaticResource MyMultiValueConverter}" Mode="TwoWay">
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="MyVisibilityDependencyProperty" Mode="TwoWay"/>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="MyBoolProperty" Mode="TwoWay"/>
</MultiBinding>
</Grid.Visibility>
Run Code Online (Sandbox Code Playgroud)
MyVisibilityDependencyProperty是依赖项属性.MyBoolProperty是一个普通的属性.MyMultiValueConverter的实现是重要的:
public class MyMultiValueConverter: IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
//Not interesting
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return new[] { value, Binding.DoNothing};
}
}
Run Code Online (Sandbox Code Playgroud)
现在的情景:我做的很好.触发ConvertBack-Method的调用,这意味着我在那里遇到了一个断点.之后我在MyVisibilityDependencyProperty的OnPropertyChangedCallback中找到了一个断点.在那里我可以看到MyVisibilityDependencyProperty的新值是ConvertBack-Method中设置的值.
现在这个问题我不明白.我将ConvertBack-Method的实现更改为:
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return new[] { value, DependencyProperty.UnsetValue};
}
Run Code Online (Sandbox Code Playgroud)
现在我遵循完全相同的方案.我做的很好.触发ConvertBack-Method的调用,这意味着我在那里遇到了一个断点.之后没有任何反应.未调用OnPropertyChangedCallback,并且不更新MyVisibilityDependencyProperty.为什么?
看起来如果数组中的一个值是DependencyProperty.UnsetValue,则停止所有值的传播.不仅是该值,还包括数组中的所有值.这受以下行为支持:
return new[] { …Run Code Online (Sandbox Code Playgroud) wpf ×5
binding ×3
c# ×3
.net ×1
bitmapimage ×1
decode ×1
image ×1
itemscontrol ×1
memory ×1
memory-leaks ×1
performance ×1
printing ×1
styles ×1
unset ×1
winforms ×1