我想避免滚动ListView
的Header
,所以我创建的自定义控制研究的名单,其中包括电网的两行(其中第一个是头和排在第二,我可以放置ListView
,但我已经成功地绑定与控制研究头,但我不能对数据做同样的事情。
我的ListViewCustomControl
课:
public sealed class ListViewCustomControl : Control
{
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register(
"Header", typeof(string), typeof(ListViewCustomControl),
new PropertyMetadata("custom_template_row1"));
public static readonly DependencyProperty ElementsSourceProperty =
DependencyProperty.Register(
"ElementsSource", typeof(IObservable<Type>), typeof(ListViewCustomControl),
null);
public ListViewCustomControl()
{
DefaultStyleKey = typeof(ListViewCustomControl);
}
public string Header
{
get { return (string)GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}
public IObservable<Type> ElementsSource
{
get { return (IObservable<Type>)GetValue(ElementsSourceProperty); }
set { SetValue(ElementsSourceProperty, value); }
}
}
我的控件样式:
<Style TargetType="local:ListViewCustomControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ListViewCustomControl">
<Grid Background="BlueViolet">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="{TemplateBinding Header}"></TextBlock>
<ListView Grid.Row="1"
ItemsSource="{TemplateBinding ElementsSource}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding TypeVar}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在xaml文件中使用的示例:
<local:ListViewCustomControl
Header="yolo" ElementsSource="{Binding data}">
</local:ListViewCustomControl>
发送的数据和Type
定义
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
var data = new List<Type>();
for (var i = 0; i < 123; i++) data.Add(new Type(i));
defaultViewModel["data"] = data;
}
public class Type
{
public int TypeVar { get; set; }
public Type(int i)
{
TypeVar = i;
}
}
正如我在一开始提到的。标头属性运行良好,但我也无法使数据绑定运行良好...
更新
我注意到在调试器中,SetValue
from的功能XamlTypeInfo.g.cs
已被调用一次,仅用于设置Header
值。
更新
我已更改IObservable
为IList
,更重要的默认值是ElementsSourceProperty
,它适用于默认值,但我仍然无法更改此值...
public static readonly DependencyProperty ElementsSourceProperty =
DependencyProperty.Register(
"ElementsSource", typeof(IList<Type>), typeof(ListViewCustomControl),
new PropertyMetadata(
new List<Type>(3)
{
new Type(4), new Type(5), new Type(6)
}));
尝试更改的类型ElementsSource
:
IObservable<>
至:
IList<>
像这样:
public static readonly DependencyProperty ElementsSourceProperty =
DependencyProperty.Register(
"ElementsSource", typeof(IList<Type>), typeof(ListViewCustomControl),
null);
public IList<Type> ElementsSource
{
get { return (IList<Type>)GetValue(ElementsSourceProperty); }
set { SetValue(ElementsSourceProperty, value); }
}
为了进行测试,请尝试设置ListView
名称,并在代码后边写以下行:
MyListView.ElementsSource = data;
Version with Binding
在这种情况下,您必须指定DataContext
,希望我们将其列出:
XAML
<local:ListViewCustomControl Header="yolo"
ElementsSource="{Binding TestList}" />
Code-behind
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var testData = new TestData();
testData.TestList = new List<Type>();
for (var i = 0; i < 123; i++)
testData.TestList.Add(new Type(i));
this.DataContext = testData;
}
}
public class TestData
{
public List<Type> TestList
{
get;
set;
}
}
Some notes
您可以选择的类型IEnumerable
,因为它是所有集合的基类。
创建依赖项属性时,可以跳过PropertyMetadata
,在这种情况下,系统将自动为此类型分配一个默认值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句