[英]Displaying customised Item in ItemsControl
我創建了一個(略微)擴展的Checkbox,該復選框具有IsError屬性(基本用於在不滿足某些條件時將復選框的顏色更改為紅色),如下所示:
public class MyCheckBox : CheckBox
{
public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register("IsError", typeof(bool), typeof(MyCheckBox), new UIPropertyMetadata(false));
public MyCheckBox() : base()
{
IsError = false;
IsCorrect = false;
}
public bool IsError
{
get { return (bool)GetValue(IsErrorProperty); }
set { SetValue(IsErrorProperty, value); }
}
}
此控件與以下樣式關聯:
<Style TargetType="{x:Type local:MyCheckBox}">
<Setter Property="FontSize" Value="14"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Margin" Value="0,2,8,2"/>
<Setter Property="BorderBrush" Value="Yellow"/>
<Setter Property="BorderThickness" Value="2"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="LightGray"/>
<Setter Property="BorderThickness" Value="0"/>
</Trigger>
<Trigger Property="IsError" Value="True">
<Setter Property="BorderBrush" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
一切都很好,如果我創建了這些復選框之一,它將正確響應IsError綁定。
我遇到的問題是將它們放在ItemsControl中時。 如果我創建一個非常基本的項目控件並手動設置值,那么它將起作用-顯示項目列表,並且IsError和IsChecked屬性按預期方式工作。
<ItemsControl Grid.Column="3" Grid.Row="8" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="0">
<local:MyCheckBox Content="Item1"/>
<local:MyCheckBox Content="Item2" IsChecked="True"/>
<local:MyCheckBox Content="Item3" IsError="True"/>
<local:MyCheckBox Content="Item4" IsChecked="True" IsError="True"/>
</ItemsControl>
但是,如果我對項目列表使用綁定,那么Content和IsChecked屬性將按預期方式工作,但是IsError屬性將被完全忽略,那么我將邊框變為紅色。
<ItemsControl Grid.Column="3" Grid.Row="8" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="0">
ItemsSource="{Binding MyThings}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:MyCheckBox Content="{Binding Path=ColorName}" IsChecked="{Binding Path=IsPresent}" IsError="{Binding Path=IsError}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我知道IsError綁定值是正確的(我通過包含與顯示正確的True / False信息的相同值綁定的Label進行了測試),但是我無法解決如何使自定義Checkbox正確顯示的問題; 好像顯示的項目正在使用標准復選框而不是自定義版本。
作為參考,控件綁定到的項目列表如下:
public class Thing : INotifyPropertyChanged
{
#region INotifyPropertyChanged
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
private string colorName;
public string ColorName
{
get { return colorName; }
set { colorName = value; RaisePropertyChanged("ColorName"); }
}
private bool isPresent;
public bool IsPresent
{
get { return isPresent; }
set { isPresent = value; RaisePropertyChanged("IsPresent"); }
}
private bool isError;
public bool IsError
{
get { return isError; }
set { isError = value; RaisePropertyChanged("IsError"); }
}
public CastorClip(string colorName)
{
ColorName = colorName;
IsPresent = false;
IsError = false;
}
}
private ObservableCollection<Thing> myThings = new ObservableCollection<Thing>();
public ObservableCollection<Thing> MyThings
{
get { return myThings; }
set { myThings = value; RaisePropertyChanged("MyThings"); }
}
我想念什么? 我該如何進行這項工作?
事實證明,這是自定義復選框的構造函數中的一個問題,在該構造函數中,IsError屬性設置為覆蓋綁定表達式。 我也會在這里讓其他可能性出現,以便確實進入WPF這些常見陷阱的其他人可以立即檢查它們。
默認情況下,屬性IsError沒有綁定兩種方式,請更改:
new UIPropertyMetadata(false)
至:
new FrameworkPropertyMetadata {
BindsTwoWayByDefault = true,
DefaultValue = false
}
另外,將其添加到復選框的靜態構造函數中,以便它將尋求特定於您的復選框的默認樣式:
static MyCheckBox ()
{
DefaultStyleKeyProperty.OverrideMetadata (typeof (MyCheckBox), new FrameworkPropertyMetadata (typeof (MyCheckBox)));
}
確保檢查如何將資源添加到您的應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.