我有一个ListBox,它绑定了一个ObservableCollection.
每个ListBoxItem都显示一个DataTemplate.我有一个按钮DataTemplate,当单击时,需要引用ObservableCollection它的部分DataTemplate的成员.我无法使用该ListBox.SelectedItem属性,因为单击该按钮时该项目未被选中.
所以要么:我需要弄清楚ListBox.SelectedItem当鼠标悬停时,或者单击按钮时如何正确设置.或者我需要找出另一种方法来获取ListBoxItem对该按钮所属的CLR对象的引用.第二个选项看起来更干净,但无论哪种方式都可以.
简化的代码段如下:
XAML:
<DataTemplate x:Key="postBody">
<Grid>
<TextBlock Text="{Binding Path=author}"/>
<Button Click="DeleteButton_Click">Delete</Button>
</Grid>
</DataTemplate>
<ListBox ItemTemplate="{StaticResource postBody}"/>
Run Code Online (Sandbox Code Playgroud)
C#:
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("Where mah ListBoxItem?");
}
Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用了AvalonEdit,它基于WPF和MVVM.阅读这篇文章后,我创建了以下课程:
public class MvvmTextEditor : TextEditor, INotifyPropertyChanged
{
public static DependencyProperty DocumentTextProperty =
DependencyProperty.Register("DocumentText",
typeof(string), typeof(MvvmTextEditor),
new PropertyMetadata((obj, args) =>
{
MvvmTextEditor target = (MvvmTextEditor)obj;
target.DocumentText = (string)args.NewValue;
})
);
public string DocumentText
{
get { return base.Text; }
set { base.Text = value; }
}
protected override void OnTextChanged(EventArgs e)
{
RaisePropertyChanged("DocumentText");
base.OnTextChanged(e);
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
Run Code Online (Sandbox Code Playgroud)
并使用以下XAML来使用此控件:
<avalonedit:MvvmTextEditor x:Name="xmlMessage"> …Run Code Online (Sandbox Code Playgroud) using System.IO;
class test
{
public static void Main()
{
string path=@"c:\mytext.txt";
if(File.Exists(path))
{
File.Delete(path);
}
FileStream fs=new FileStream(path,FileMode.OpenOrCreate);
StreamWriter str=new StreamWriter(fs);
str.BaseStream.Seek(0,SeekOrigin.End);
str.Write("mytext.txt.........................");
str.WriteLine(DateTime.Now.ToLongTimeString()+" "+DateTime.Now.ToLongDateString());
string addtext="this line is added"+Environment.NewLine;
File.AppendAllText(path,addtext); //Exception occurrs ??????????
string readtext=File.ReadAllText(path);
Console.WriteLine(readtext);
str.Flush();
str.Close();
Console.ReadKey();
//System.IO.IOException: The process cannot access the file 'c:\mytext.txt' because it is //being used by another process.
// at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
}
}
Run Code Online (Sandbox Code Playgroud) 有没有办法直接设置.TextWpf ComboBox控件的属性?
我的组合框绑定到一个List<T>集合,但是当我尝试.Text在DropDownClosed事件中设置属性时,它完全忽略它.
ObservableCollection实现INotifyCollectionChanged和INotifyPropertyChanged.
我了解通过集合的事件
可以向消费者通知添加,删除(+清除)和项目替换CollectionChanged,并且如果项目PropertyChanged实现自己,则可以使用项目事件
监视现有项目中的更新INotifyPropertyChanged.
我从别人那里读到你不能在集合的事件上注册,
PropertyChanged因为它是readonly.
那么它的目的是什么,我们可以利用它做什么?
这里和那里的评论似乎让讨论变得混乱,暗示着ObservableCollection实现两个接口的神奇之处在于允许同时收集集合和项目内容更改,而这是不正确的(许多示例都忽略了这一点)绑定到一个列表框,在项目内容更改后神奇地更新,建议集合通知列表框).
实际上,这个系列的唯一优势似乎是实现INotifyCollectionChanged.ObservableCollection与其他集合相比,处理项目属性更改似乎并不容易:只有当项目实现时INotifyPropertyChanged,它们可能不会执行,并且如果用户设法独立于集合挂钩此事件,则可能.
它是否正确?
我有一个实现的viewmodel INotifyPropertyChanged.在这个viewModel上是一个名为的属性SubGroupingView.此属性绑定到组合框的选定项.当我更改组合框时,源属性正在更新,但是当我更改源属性或初始化控件时,combobox.selectedItem它不反映属性中存在的内容.
以下是一些可以帮助您入门的代码:
<ComboBox Grid.Column="3" Grid.Row="1"
Margin="0,1,4,1"
SelectedItem="{Binding Path=SubGroupingView, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, diag:PresentationTraceSources.TraceLevel=High}"
ItemsSource="{Binding Columns}"
DisplayMemberPath="DisplayName">
Run Code Online (Sandbox Code Playgroud)
该属性引发PropertyChanged事件,TraceSource输出显示绑定检测到它并传输了值,只是组合框没有反映它.任何想法都会受到欢迎!
编辑:
跟踪源的输出是这样的:
System.Windows.Data Warning: 91 : BindingExpression (hash=23631369): Got PropertyChanged event from ReportViewModel (hash=52844413)
System.Windows.Data Warning: 97 : BindingExpression (hash=23631369): GetValue at level 0 from ReportViewModel (hash=52844413) using RuntimePropertyInfo(SubGroupingView): DataColumnViewModel (hash=58231222)
System.Windows.Data Warning: 76 : BindingExpression (hash=23631369): TransferValue - got raw value DataColumnViewModel (hash=58231222)
System.Windows.Data Warning: 80 : BindingExpression (hash=23631369): TransferValue - implicit converter produced DataColumnViewModel (hash=58231222) …Run Code Online (Sandbox Code Playgroud) 我试图让我的树视图按照它们是什么来组合类似项目的集合.为了保持通用性,我的对象层次结构可能如下所示:
现在我的TreeView显示这些对象与对象模型完全一样,但我想要做的是为每个对象类型插入一个TreeView节点,使它看起来像这样:
我在这里看到一个类似的问题,有人建议有两个单独的,HierarchicalDataTemplates所以我创建了一个'对象组#1'级别,其中包含一个带有类型列表的TreeView,但这真是笨拙,因为它是一个完整的单独的TreeView里面一些节点.我也一直在尝试使用a CollectionViewSource来过滤掉每个类别中的项目,但这对我来说并不是很好,因为我无法弄清楚如何显示它们.
我想我的问题归结为:我如何组成一个HierarchicalDataTemplate小组呢?如果有人能指出我正确的方向,我会非常感激.
我可以发布一些代码,如果有人想看,但我真的只是想弄清楚如何做我想要的,所以我的代码现在只是一个非常直接的数据绑定树视图.
我有一个ListView控件绑定到ListCollectionViewViewModel中的a.
我想尝试将这些项目分组,但遇到一些问题.
我在VM中设置属性分组以开始,然后添加一个GroupStyle.
C#:
ListCollectionView.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
Run Code Online (Sandbox Code Playgroud)
XAML:
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
Run Code Online (Sandbox Code Playgroud)
但是列表现在只是类别名称,无法查看项目本身.
我并不完全理解这里发生了什么.当我为GroupStyle我真正绑定的内容创建一个模板时?除此之外还有其他房产Name吗?
我刚刚添加GroupStyle到我已经创建的ListView中,例如我包含了一个ItemTemplate.那是什么东西搞乱了GroupStyle吗?
如果列表中的Items属于另一个类,并且我不想根据它们所属的类的实例(它有一个ID)进行分组.然后我将组名作为此父类的属性.那可能吗?
部分解决方案:
问题在于ListView上应用的样式.我不知道风格如何干扰.
完整的解决方案
我没有ItemsPresenter在我的列表框中ControlTemplate使用选择使用IsItemsHost设置为的Panel true.似乎ItemsPresenter必须用于GroupStyling才能正常工作.
我正在尝试将MenuItem的命令绑定到包含在其中的命令UserControl.DataContext.我发现了几个类似的问题,但根据它们的解决方案对我没有意义:
<UserControl ...>
<UserControl.Resources>
<DataTemplate x:Key="TileItemStye">
<Grid Width="100" Height="100">
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove"
Command="{Binding DataContext.RemoveItem,
RelativeSource={RelativeSource FindAncestor,
AncestorType=UserControl}}">
</MenuItem>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid>
<ListView ItemsSource="{Binding Path=Files}"
ItemTemplate="{DynamicResource TileItemStye}" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</Grid>
Run Code Online (Sandbox Code Playgroud)
UserControl的DataContext是带有ICommandRemoveItem和ObservableCollection<FileViewModel>Files的ViewModel .
我在XAML中有一个ItemsControl,我为每个组显示一个扩展器,这样我就可以扩展/折叠组.我想保持IsExpanded属性的状态(以及可能与组头的显示有关的其他设置).通常你只有一个包含属性的类并绑定到它.但是,该组的数据上下文是CollectionViewGroup.现在这个类不是很有帮助,因为它只给你Name属性和组中的项目(如果你只是想要一个标题,这可能很好,并且可能根据组中的项目数量或它们显示某种度量标准内容但不是,如果您只想存储有关组头UI状态的自定义数据).我想要做的是从这个类派生并将其他属性添加到我的派生类并绑定到它.但似乎没有任何简单的方法可以做到这一点.小组生成的所有细节似乎都隐藏在内部课程中,这非常令人沮丧.有没有人沿着实现ICollectionView自己的路线走下去(因此推测所有其他相关课程)?复制一切似乎是一项巨大的工作ListCollectionView只是为了能够创建一个自定义CollectionViewGroup类并绑定到它!谢谢.
wpf ×9
c# ×4
.net ×2
combobox ×2
data-binding ×2
xaml ×2
append ×1
avalonedit ×1
binding ×1
datacontext ×1
datatemplate ×1
file-io ×1
grouping ×1
listbox ×1
listboxitem ×1
listview ×1
mvvm ×1
selecteditem ×1
treeview ×1