- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试简单地将 IsSelected
属性与我类(class)中的 IsSelected
字段进行数据绑定(bind)。但是在我更改代码中的值后,它不会更改属性,单击 ListBoxItem
也不会更改字段值。
XAML:
<FlipView ItemsSource="{Binding Source={StaticResource itemsViewSource}}" ... >
<FlipView.ItemTemplate>
<DataTemplate>
<UserControl Loaded="StartLayoutUpdates"
Unloaded="StopLayoutUpdates">
<!-- other controls -->
<ListBox Grid.Row="1" Grid.ColumnSpan="3"
SelectionMode="Multiple" VerticalAlignment="Center"
ItemsSource="{Binding Answers}">
<ListBox.Resources>
<local:LogicToText x:Key="logToText" />
</ListBox.Resources>
<!-- bind IsSelected only in one way from
code to content -->
<ItemsControl.ItemTemplate>
<DataTemplate>
<ListBoxItem
IsSelected="{Binding IsSelected, Mode=TwoWay, Converter={StaticResource logToText}}"
Content="{Binding IsSelected, Mode=TwoWay, Converter={StaticResource logToText}}">
</ListBoxItem>
</DataTemplate>
</ItemsControl.ItemTemplate>
<!-- not working at all
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected"
Value="{Binding IsSelected, Mode=TwoWay}"/>
<Setter Property="Content"
Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>
</ListBox.Resources>-->
</ListBox>
</UserControl>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
代码:
答案
private ObservableCollection<PrawoJazdyDataAnswer> _answers =
new ObservableCollection<PrawoJazdyDataAnswer>();
public ObservableCollection<PrawoJazdyDataAnswer> Answers
{
get
{
return this._answers;
}
}
单项(答案)
public class PrawoJazdyDataAnswer : NPCHelper// PrawoJazdy.Common.BindableBase
{
public PrawoJazdyDataAnswer(String ans, bool ansb)
{
this._ans = ans;
this._isSelected = ansb;
}
public override string ToString()
{
return _isSelected.ToString(); //Only For debug purposes
//normally return _ans
}
private string _ans;
public string Ans
{
get { return this._ans; }
//set { this.SetProperty(ref this._ans, value); }
}
private bool _isSelected;
public bool IsSelected
{
get { return this._isSelected; }
set
{
_isSelected = value;
FirePropertyChanged("IsSelected");
//this.SetProperty(ref this._isSelected, value);
}
}
}
FirePropertyChanged
public class NPCHelper : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void FirePropertyChanged(string prop)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
转换器(有时似乎需要而其他人不需要...,我尝试了来自不同教程/示例的 ~10 种方法)
public class LogicToText : IValueConverter
{
/// <summary>
///
/// </summary>
public object Convert(object value, Type targetType,
object parameter, string language)
{
//if (value == null || (bool)value == false)
// return "False";
return value.ToString();
}
/// <summary>
///
/// </summary>
public object ConvertBack(object value, Type targetType,
object parameter, string language)
{
return value.ToString().Contains("True") ? true : false;
}
提前致谢,对不起我的英语(还在学习)。
@编辑感谢您的快速回复。
出于测试目的,我创建了一个按钮和文本 block :
<Button Click="spr" >Sprawdź</Button>
<TextBlock Text="{Binding Answers[0].IsSelected, Mode=TwoWay}" > </TextBlock>
在其他控件部分(在列表框上方,但在FlipView
中)点击方式
private void spr(object sender, RoutedEventArgs e)
{
var ans = ((PrawoJazdyDataQuestion)this.flipView.SelectedItem).Answers;
foreach (var item in ans)
item.IsSelected = item.IsSelected ? false : true;
}
正如我所写,当我从代码端更改数据时,它会更改元素的内容,但不会更改 ListBoxItem
的外观。如果我只是在 ListBox
上选择它,它不会更改 TextBlock
中的数据,也不会更改 ListBox
本身中的数据。
@edit2 修正拼写错误...
最佳答案
要更改 ListBoxItem
的 IsSelected
属性,您需要更改 ListBox.ItemContainerStyle
。看这里:
<ListBox Grid.Row="1" Grid.ColumnSpan="3"
SelectionMode="Multiple" VerticalAlignment="Center"
ItemsSource="{Binding Answers}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding IsSelected}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
由于绑定(bind)模式是 TwoWay
,选择和取消选择 ListBoxItem
会动态更改项目的内容以显示 True
或错误
。
另请注意我如何将 ListBox.ItemTemplate
更改为 TextBlock
而不是 ListBoxItem
。 ItemTemplate
定义了 ListBoxItem
的内容,因此通常使用某种类型的内容控件。下面是不同布局的 UI 结构(可以使用 WPF 树可视化工具查看)。
ListBoxItem
作为 ItemTemplate
:
TextBlock
作为 ItemTemplate
:
编辑
另请注意,我删除了 IValueConverter
。由于您的源属性和目标属性都是 bool
,因此在这种情况下不需要转换器。尝试删除转换器引用以查看是否可以解决问题。
关于c# - listbox isSelected DataTemplate 中的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12466967/
我正在使用 DataTemplate 将 View 应用于 ViewModel。我有一种情况,当 View 的一个实例(DataTemplate)中发生某件事时,我需要在所有其他实例中采取行动。 我已
如何从 DataTemplate 派生新类,然后用它代替 DataTemplate? 例如,在 C# 中: public class DerivedDataTemplate : DataTemplat
我有以下xaml; 当我使用 Node 模板显示 No
我想在 dataTemplate 中使用 dataTemplale。我想像这样在列表框中显示数据: 这就是我得到的。它不起作用。 cl
大家好,我正在尝试更改 dataGrid 中列的模板,但我找不到在 XAML 中执行此操作的方法。我正在尝试通过这种方式来做
wpf 应用程序中的默认 DataTemplate 显示 .ToString() 方法的结果。我正在开发一个默认 DataTemplate 不显示任何内容的应用程序。 我试过:
我发现自己有点束手无策……有束缚……呵呵……(跛脚) 无论如何...我需要引用主 ViewModel 的属性,但来自 DataTemplate ,它本身在另一个 DataTemplate 中... 看
我已经为我喜欢的项目中的一些数据类型创建了几个数据模板。这些数据模板真的很酷,因为它们像魔术一样工作,无论何时何地出现在 UI 中,都神奇地改变了数据类型实例的外观。现在我希望能够在一个特定的列表框中
这个问题在这里已经有了答案: 11年前关闭。 Possible Duplicate: Datatemplate inheritance 我有几个不是子类的数据类型,它们也不共享一个接口(interfa
我有一个用代码隐藏在 XAML 中定义的 ResouceDictionary。我需要使用鼠标事件和数据绑定(bind)定义一些特定于 View 的行为,为此我需要访问在 DataTemplate 中定
我有一个用代码隐藏在 XAML 中定义的 ResouceDictionary。我需要使用鼠标事件和数据绑定(bind)定义一些特定于 View 的行为,为此我需要访问在 DataTemplate 中定
我创建了一个 DataTemplateSelector,它使用一组已知接口(interface)进行初始化。如果传递到选择器的项目实现了这些接口(interface)之一,则返回关联的数据模板。 首先
是否可以将数据模板用于没有 ListBox 或其他项目控件的单个项目? 我有一个数据模板,我想在 xaml 中实例化,而不是在列表中,只是在边框内,并设置其数据上下文。 类似(伪):
在我的 MainWindow.xaml 中,我对 ResourceDictionary 有以下引用: 在 Main
我在数据模板中有 WPF ComboBox(列表框中有很多组合框),我想处理输入按钮。如果它是例如,那将很容易。一个按钮 - 我会使用命令 + 相对绑定(bind)路径等。不幸的是,我不知道如何使用命
我想对 ItemsCollection 进行数据绑定(bind),但不是渲染集合项,而是渲染通过集合项上的属性到达的子对象。 更具体地说:这将是游戏的 2D map 查看器(尽管在当前状态下它还不是
我使用 MasterDetailsView控制。它有 MasterHeaderTemplate属性(property)。我想添加一个 TextBox以便执行元素搜索。我不明白该怎么做。因为DataTe
我为自己设计的一个非常基本的 WPF 练习遇到了一个奇怪的问题,即从 ViewModel 动态填充菜单。给定以下主窗口标记:
在设计新的 WPF 应用程序时,我注意到在使用 DataTemplates 对控件进行数据绑定(bind)期间不会抛出异常。为了测试,我用尽可能少的逻辑编写了以下简单的用户控件。我正在使用在 WIN7
我有一个关于 this one 的问题:我正在尝试将一个事件附加到我的 StackPanel,但在使用 XamlReader 时它似乎没有连接。我无法调用 ChildItem_Load 方法。有谁知道
我是一名优秀的程序员,十分优秀!