我有一个CustomControl包含ListBox:
<UserControl x:Class="WpfApplication1.CustomList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<ListBox Name="listBox1" ItemsSource="{Binding ListSource}" />
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
我ItemsSource使用Code Behind中的属性绑定:
public partial class CustomList : UserControl, INotifyPropertyChanged
{
public CustomList( )
{
InitializeComponent( );
}
public ObservableCollection<object> ListSource
{
get
{
return (ObservableCollection<object>)GetValue( ListSourceProperty );
}
set
{
base.SetValue(CustomList.ListSourceProperty, value);
NotifyPropertyChanged( "ListSource" );
}
}
public static DependencyProperty ListSourceProperty = DependencyProperty.Register(
"ListSource",
typeof( ObservableCollection<object> ),
typeof( CustomList ),
new PropertyMetadata( OnValueChanged ) );
private …Run Code Online (Sandbox Code Playgroud) 我有这个Custom UserControl,它有一个List和一个Button:
<UserControl x:Class="WpfApplication1.CustomList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<ListBox Name="listBox1" ItemsSource="{Binding ListSource}" HorizontalAlignment="Right" Width="174" />
<Button Name="ButtonAdd" Content="Add" HorizontalAlignment="Left" Width="101" />
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
后面的代码有一个Type IEnumerable的DependencyProperty和一个Button的处理程序(OnAdd):
public partial class CustomList : UserControl
{
public CustomList( )
{
InitializeComponent( );
ButtonAdd.Click += new RoutedEventHandler( OnAdd );
}
private void OnAdd( object sender, EventArgs e )
{
IList<object> tmpList = this.ListSource.ToList( );
Article tmpArticle = new Article( );
tmpArticle .Name = "g";
tmpList.Add(tmpArticle );
ListSource = (IEnumerable<object>) tmpList;
} …Run Code Online (Sandbox Code Playgroud) 如果 SomeProperty 值为 10,我想更改 ListBox 中第一项的边距,而无需代码隐藏。这是我到目前为止:
<ListBox x:Class="Windows.CustomList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Windows"
mc:Ignorable="d" x:Name="MyList"
d:DesignHeight="300" d:DesignWidth="300">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=SomeProperty}" Value="10"/>
<Condition Binding="{Binding Path=Items.Count, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}}" Value="1" />
</MultiDataTrigger.Conditions>
<Setter Property="Margin">
<Setter.Value>
<Thickness Left="500"/>
</Setter.Value>
</Setter>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<local:ListBoxItemCustomTemplate/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
当我尝试这种方法时,我得到:
System.Windows.Data 错误:4:无法找到引用“RelativeSource FindAncestor、AncestorType='ListBox'、AncestorLevel='1'”的绑定源。BindingExpression:Path=Items.Count; 数据项=空;目标元素是 'ListBox' (Name=''); 目标属性是“NoTarget”(类型“Object”)
如果我只有第一个条件,它会正确应用边距。我尝试的另一种方法是使用 ElementName:
这种方法不会出现任何错误,但也不起作用。
任何帮助将非常感激。