C# 如何使用ElementFlow?
我对WPF比较陌生,我来自WinForms背景,我正在尝试实现coverflow,但我不完全理解这个示例,从我所看到的,我将图像的路径添加到C# 如何使用ElementFlow?,c#,wpf,xaml,elementflow,C#,Wpf,Xaml,Elementflow,我对WPF比较陌生,我来自WinForms背景,我正在尝试实现coverflow,但我不完全理解这个示例,从我所看到的,我将图像的路径添加到StringCollection 这就是我现在拥有的: public MainWindow() { InitializeComponent(); elementFlow1.Layout = new CoverFlow(); StringCollection itemssource = new StringCollection();
StringCollection
这就是我现在拥有的:
public MainWindow()
{
InitializeComponent();
elementFlow1.Layout = new CoverFlow();
StringCollection itemssource = new StringCollection();
itemssource.Add(@"Images\1.png");
itemssource.Add(@"Images\2.png");
elementFlow1.SelectedIndex = 0;
elementFlow1.ItemsSource = itemssource;
}
我在XAML中定义了ElementFlow
,如下所示:
<fluidkit:ElementFlow Grid.Row="1" Height="194" Name="elementFlow1" VerticalAlignment="Top" Width="503" />
瞧,当我运行它时,什么也没发生
有人能解释一下我应该如何使用
ElementFlow
?这个例子并没有很好地“解释”它。你错过了一个关键步骤。ElementFlow
控件显示UIElement
s,而不是字符串。您有一个字符串列表,其中包含图像文件的逻辑文件位置。现在需要将该字符串集合转换为DataTemplate
s的集合。如果查看示例xaml文件,您将看到以下部分:
<DataTemplate x:Key="TestDataTemplate"
DataType="{x:Type sys:String}">
<Border x:Name="ElementVisual"
Background="White"
Padding="5"
BorderThickness="5"
BorderBrush="LightGray"
Grid.Row="0">
<Image Source="{Binding}"
Stretch="Fill" />
</Border>
</DataTemplate>
该部分实质上接受字符串输入并将其转换为数据模板。这是通过将ItemTemplate
属性设置为此DataTemplate
资源来完成的
在XAML中操作此控件可能比在代码后面操作要好。那样的话事情就容易多了
希望这能有所帮助。我建议提供以下示例:
这是我的mod,项目名为:ElementFlowExample
namespace ElementFlowExample
{
#region Using Statements:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FluidKit.Controls;
using FluidKit.Showcase.ElementFlow;
#endregion
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
#region Fields:
private StringCollection _dataSource;
private LayoutBase[] _layouts = {
new Wall(),
new SlideDeck(),
new CoverFlow(),
new Carousel(),
new TimeMachine2(),
new ThreeLane(),
new VForm(),
new TimeMachine(),
new RollerCoaster(),
new Rolodex(),
};
private Random _randomizer = new Random();
private int _viewIndex;
#endregion
#region Properties:
#endregion
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_elementFlow.Layout = _layouts[3];
_currentViewText.Text = _elementFlow.Layout.GetType().Name;
_selectedIndexSlider.Maximum = _elementFlow.Items.Count - 1;
_elementFlow.SelectionChanged += EFSelectedIndexChanged;
_elementFlow.SelectedIndex = 0;
_dataSource = FindResource("DataSource") as StringCollection;
}
private void EFSelectedIndexChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine((sender as ElementFlow).SelectedIndex);
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.F12)
{
_viewIndex = (_viewIndex + 1) % _layouts.Length;
_elementFlow.Layout = _layouts[_viewIndex];
_currentViewText.Text = _elementFlow.Layout.GetType().Name;
}
}
private void ChangeSelectedIndex(object sender, RoutedPropertyChangedEventArgs<double> args)
{
_elementFlow.SelectedIndex = (int)args.NewValue;
}
private void RemoveCard(object sender, RoutedEventArgs args)
{
if (_elementFlow.Items.Count > 0)
{
_dataSource.RemoveAt(_randomizer.Next(_dataSource.Count));
// Update selectedindex slider
_selectedIndexSlider.Maximum = _elementFlow.Items.Count - 1;
}
}
private void AddCard(object sender, RoutedEventArgs args)
{
Button b = sender as Button;
int index = _randomizer.Next(_dataSource.Count);
if (b.Name == "_regular")
{
_dataSource.Insert(index, "Images/01.jpg");
}
else
{
_dataSource.Insert(index, string.Format("Images/{0:00}", _randomizer.Next(1, 12)) + ".jpg");
}
// Update selectedindex slider
_selectedIndexSlider.Maximum = _elementFlow.Items.Count - 1;
}
} // END of Class...
} // END of Namespace...
namespace元素流示例
{
#区域使用语句:
使用制度;
使用系统诊断;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Media.Media3D;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用FluidKit.控件;
使用FluidKit.Showcase.ElementFlow;
#端区
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
#区域字段:
私有StringCollection_数据源;
专用布局库[]\u布局={
新墙(),
新的SlideDeck(),
新CoverFlow(),
新转盘(),
新的TimeMachine2(),
新三线(),
新VForm(),
新的计时器(),
新过山车(),
新Rolodex(),
};
私有随机_随机化器=新随机();
私有int_视图索引;
#端区
#区域属性:
#端区
公共主窗口()
{
初始化组件();
}
已加载私有无效窗口(对象发送器、路由目标)
{
_elementFlow.Layout=_layouts[3];
_currentViewText.Text=\u elementFlow.Layout.GetType().Name;
_selectedIndexSlider.Maximum=\u elementFlow.Items.Count-1;
_elementFlow.SelectionChanged+=EFSelectedIndexChanged;
_elementFlow.SelectedIndex=0;
_dataSource=FindResource(“dataSource”)作为StringCollection;
}
私有无效EFSelectedIndexChanged(对象发送方,SelectionChangedEventArgs e)
{
Debug.WriteLine((发送方为ElementFlow)。选择了索引);
}
受保护的覆盖无效OnKeyDown(KeyEventArgs e)
{
如果(e.Key==Key.F12)
{
_viewIndex=(_viewIndex+1)%\u layouts.Length;
_elementFlow.Layout=_layouts[_viewIndex];
_currentViewText.Text=\u elementFlow.Layout.GetType().Name;
}
}
private void ChangeSelectedIndex(对象发送方,RoutedProperty ChangedEventArgs参数)
{
_elementFlow.SelectedIndex=(int)args.NewValue;
}
私有void RemoveCard(对象发送方,RoutedEventArgs参数)
{
如果(_elementFlow.Items.Count>0)
{
_RemoveAt(_randomizer.Next(_dataSource.Count));
//更新selectedindex滑块
_selectedIndexSlider.Maximum=\u elementFlow.Items.Count-1;
}
}
私有无效添加卡(对象发送方,路由目标参数)
{
按钮b=发送器为按钮;
int index=\u randomizer.Next(\u dataSource.Count);
如果(b.Name==“\u常规”)
{
_插入(索引,“Images/01.jpg”);
}
其他的
{
_Insert(index,string.Format(“Images/{0:00}”,_randomizer.Next(1,12))+“.jpg”);
}
//更新selectedindex滑块
_selectedIndexSlider.Maximum=\u elementFlow.Items.Count-1;
}
}//下课了。。。
}//命名空间的结尾。。。