[英]Changing checkbox of column type DataGridCheckBoxColumn using DataTrigger
[英]DataTrigger type to textbox/checkbox/
我有一個ItemsControl,在其中顯示不同的屬性和值,一側顯示名稱,另一側顯示文本框。 ItemsSource是自定義類的對象的集合,該類具有Name,Value和PropertyType屬性(使用反射propertyinfo)
現在,我希望通過檢測該屬性是否為bool類型來改進此功能,該屬性將顯示一個復選框而不是一個文本框。 是否可以使用DataTrigger?
我使用控件進行半工作,該控件根據類型將模板設置為文本框或復選框,但是當我嘗試“跳至”下一個文本框或復選框時,它將集中具有文本框/復選框的控件首先,並且僅在另一個“標簽”之后,它才對包含的文本框/復選框/進行聚焦。
因此,如果有人知道解決方案,將不勝感激!
您可以使用DataTemplate根據Value屬性類型選擇其他視圖。
視圖:
<ItemsControl ItemsSource="{Binding Path=Options}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<DataTemplate DataType="{x:Type System:Boolean}">
<CheckBox IsChecked="{Binding Path=.}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type System:String}">
<TextBox Text="{Binding Path=.}"/>
</DataTemplate>
</DataTemplate.Resources>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name, Mode=OneWay}"/>
<ContentControl Content="{Binding Path=Value}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
ViewModel:
public class MainViewModel
{
public ArrayList Options { get; set; }
public MainViewModel()
{
Options = new ArrayList();
Options.Add(new TextProperty());
Options.Add(new BoolProperty());
}
}
public class TextProperty
{
public string Name { get; set; }
public string Value { get; set; }
public TextProperty()
{
Name = "Name";
Value = "Default";
}
}
public class BoolProperty
{
public string Name { get; set; }
public bool Value { get; set; }
public BoolProperty()
{
Name = "IsEnabled";
Value = true;
}
}
使用您已有的解決方案,並在錯誤地獲得標簽焦點的控件上將Focusable屬性設置為false。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.