我想将树视图绑定到类似这样的类:
public class Folder : Base_FileFolder
{
public Folder()
{
Folders = new ObservableCollection<Folder>();
Files = new ObservableCollection<File>();
}
public ObservableCollection<Folder> Folders { get; set; }
public ObservableCollection<File> Files { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
其他类是:
public class File : Base_FileFolder
{
}
public class Base_FileFolder : DependencyObject
{
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Base_FileFolder), new UIPropertyMetadata(""));
}
Run Code Online (Sandbox Code Playgroud)
如何创建显示"文件和文件夹"集合的树视图
我想用这样的东西:
<HierarchicalDataTemplate
DataType="{x:Type model:Folder}" …Run Code Online (Sandbox Code Playgroud) 我正在为以下问题寻找一个优雅的解决方案.
假设我们有一个具有以下布尔属性的(View)模型:
接下来,我在表面上有5个控件,只有在满足基于这些属性的条件时才能看到它们.当然,只要更新其中一个属性,就应该立即传播更改:
到目前为止我提出的唯一解决方案是使用MultiValueConverters.
ControlA的示例:
<ControlA>
<ControlA.Visibility>
<MultiBinding Converter={StaticResource ControlAVisibilityConverter}>
<Binding Path="Alpha"/>
<Binding Path="Beta"/>
<Binding Path="Gamma"/>
</MultiBinding>
</ControlA.Visibility>
</ControlA>
Run Code Online (Sandbox Code Playgroud)
此ControlAVisibilityConverter检查条件"Alpha &&(Beta || Gamma)"并返回适当的值.
它确实有用......好吧..但也许你可以想出一个更优雅的解决方案?
谢谢TwinHabit
我有这个用例非常类似于Guice的机器人腿例子,除了我不知道我有多少"腿".因此,我无法使用机器人腿示例所需的注释.
我期望在带有Guice的Multibindings扩展的java.util.Set中收集所有这些"leg".
从技术上讲,在PrivateModule中,我想直接将实现作为Multibindings扩展提供的集合的元素公开.我只是不知道该怎么做.
有关参考和代码示例,请参阅此处的robot-legs示例:http://code.google.com/p/google-guice/wiki/FrequentlyAskedQuestions#How_do_I_build_two_similar_but_slightly_different_trees_of_objec
这是我的确切用例:
我有以下内容:
// Main application
public interface MyTree {...}
public interface MyInterface {
public MyTree getMyTree() {}
}
public abstract class MyModule extends PrivateModule {}
public class MyManager {
@Inject MyManager (Set<MyInterface> interfaces){ this.interfaces = interfaces }
}
public class MainModule extends AbstractModule {
public void configure () {
// Install all MyModules using java.util.ServiceLoader.
}
}
// In expansion "square.jar"
public class SquareTree implements MyTree {...}
public class SquareImplementation implements MyInterface { …Run Code Online (Sandbox Code Playgroud) 是否可以(如果是的话)将多值绑定表达式添加到资源中.
我有一个多值绑定,它在其中一个绑定中采用2个单独的绑定和转换器参数.
我必须将此绑定用于5个不同的项目,并且这些绑定标记仅在转换器参数中有所不同.休息一切都一样.
我想避免重复多绑定样板标签.
我的viewmodel中有一组变量:
public ObservableCollection<ObservableVariable> Variables { get; }= new ObservableCollection<ObservableVariable>();
Run Code Online (Sandbox Code Playgroud)
ObservableVariable类有两个属性:string Name和bool Selected; 该类实现了INotifyPropertyChanged,
我的目标是将此集合绑定到WPF视图中的清单,并使用MultiBinding实现绑定到该列表的"全选"复选框.下图说明了所需的视图.

观察下面的XAML:
<CheckBox Content="Select All" Name="SelectAllCheckbox"></CheckBox>
...
<ListBox ItemsSource="{Binding Variables}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}">
<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource LogicalOrConverter}" Mode="TwoWay">
<Binding Path="Selected"></Binding>
<Binding ElementName="SelectAllCheckbox" Path="IsChecked"></Binding>
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
LogicalOrConverter可以使用任意数量的bool; 如果有的话,返回true.
如您所见,每个复选框都绑定到viewmodel中的变量和'select all'复选框的状态.目前,一切都按预期工作除了以下内容:如果单击"全选",复选框将在视图中更新,但更改不会传播回视图模型.
注意,我的实现中的大多数事情都能正常工作 例如,如果单击某个复选框,则视图模型会正确更新.
问题更详细:
当我单击一个单独的复选框时,OnPropertyChanged事件将在其框刚刚更改的变量中触发; 转换器中的ConvertBack函数被触发; viewmodel已更新,一切正常.
但是,当我单击"全选"复选框时,视图中会更新各个复选框,但不会在任何变量中调用OnPropertyChanged,并且不会调用转换器中的ConvertBack函数.
同样相关,如果我取消选中"全选",个别支票会回到之前的状态.
更新viewmodel的唯一方法是单击各个复选框.但是,多绑定适用于视图.
我的问题是:
为什么不对复选框中的更改传播到viewmodel中的源集合
转换器:
public class LogicalOrConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
foreach (object arg …Run Code Online (Sandbox Code Playgroud) 我们目前正在将项目从3.5版转换为.NET 4.5版.
我们设置了一个文本框IsEnabled,它使用多绑定转换器进行多重绑定.每个绑定都有自己的转换器.
一切都在.NET 3.5中运行良好,但在.NET 4.5中,传递给子转换器的目标类型是object类型而不是bool.
这是一个已知的问题?MS重构多重绑定,不将目标类型传递给子转换器.
我创建了一个简化的项目来演示这个问题.我在VS2008中创建了项目,然后将其转换为VS2012和.NET 4.5.
窗口XAML:
<Window x:Class="TestMultiBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestMultiBinding"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<local:NotConverter x:Key="NotConverter"/>
<local:MultiBoolConverter x:Key="MultiBoolConverter"/>
</Window.Resources>
<StackPanel>
<TextBox>
<TextBox.IsEnabled>
<MultiBinding Converter="{StaticResource MultiBoolConverter}">
<Binding Path="ConditionOne" />
<Binding Path="ConditionTwo" Converter="{StaticResource NotConverter}"/>
</MultiBinding>
</TextBox.IsEnabled>
</TextBox>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;
namespace TestMultiBinding
{
/// <summary>
/// Interaction logic for …Run Code Online (Sandbox Code Playgroud) 看一下以下XAML代码段:
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Block.TextAlignment" Value="Center"/>
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush>
<SolidColorBrush.Color>
<MultiBinding Converter="{StaticResource VAPBrushConverter}">
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}"/>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}"/>
</MultiBinding>
</SolidColorBrush.Color>
</SolidColorBrush>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.CellStyle>
Run Code Online (Sandbox Code Playgroud)
仅在滚动数据网格时才调用IValueConverter 。在DataGridCell中,有一个TextBlock,并且只有在TextBlock.Text属性为DependencyProperty.UnsetValue时,才调用IValueConverter。
有人可以告诉我何时调用IValueConverter,并且当前是否可以使用我的代码来解决此问题?澄清一下-问题是,当我在DataGrid上滚动时,背景仅由IValueConverter设置。
我有一个带有绑定功能的按钮,可以正常工作,请参见下文:
<Button x:Name="licenceFilterSet" Content="Search" Command="{Binding searchCommand}" CommandParameter="{Binding Path=Text, ElementName=licenseTextBox}" />
Run Code Online (Sandbox Code Playgroud)
现在,我意识到我还需要另一条信息,因此我也需要发送a的值check-box。我这样修改了虚拟机:
<Button x:Name="licenceFilterSet" Content="Search" Command="{Binding licenseSearchCommand}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource searchFilterConverter}">
<Binding Path="Text" ElementName="licenseTextBox" />
<Binding Path="IsEnabled" ElementName="regularExpressionCheckBox" />
</MultiBinding>
</Button.CommandParameter>
</Button>
Run Code Online (Sandbox Code Playgroud)
以下是我的多转换器:
/// <summary>
/// Converter Used for combining license search textbox and checkbox
/// </summary>
public class SearchFilterConverter : IMultiValueConverter
{
public object Convert(object[] values)
{
return new Tuple<String, bool>((String)values[0], (bool)values[1]);
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么。我收到以下错误,(它指向XAML中的MultiBinding-tag):
Cannot set MultiBinding because MultiValueConverter must be specified.
Run Code Online (Sandbox Code Playgroud) 在C#项目中,我有一个复杂的模型,它使用嵌套在列表和字典中的许多类(例如,对象A有一个实例列表B,其中包含值为实例的字典C).在我的一个页面中,使用嵌套ItemsControls 在复杂视图中显示此模型.
此外,还有一个Settings存储用户首选项的类,其中一些首选项绑定到页面上的复选框.
现在,我想将DataTemplates 中某些控件的属性绑定到模型属性和设置的组合.例如,假设C有一个属性IsBoring,并且有一个设置Settings.HideBoringStuff.我想将TextBlock表示的可见性绑定C到这些属性的明显组合.
没有丑陋的黑客,我不知道怎么做.以下是我的一些想法以及它们不起作用的原因:
使用a MultiBinding,专门用于执行此操作.但是,MultiBinding在UWP项目中不可用.
绑定到页面上的多个属性,这些属性在getter和setter中实现逻辑.这不起作用,因为我在里面DataTemplate,所以我需要这个逻辑的多个独立副本.
使用a Converter转换模型属性,将设置作为a传递ConverterProperty.但是,它ConverterProperty是n o DependencyProperty因此不能被约束.
在模型中构建所需的属性 - Settings无论如何都是单身.这感觉真的很难看,因为我会在我的模型中混合不必要的依赖关系并查看逻辑.
构建包装模型类的单独类,还存储Settings要使用的对象,然后提供组合属性.这也感觉很难看,因为我需要复制模型的整个对象层次结构.(在示例中,ViewA需要提供一个ViewBs 列表,每个列表都有一个字典,其中的值是相应的ViewCs.)
等待微软带MultiBinding回来.不幸的是,我缺乏必要的乐观态度.
在UWP应用程序中哪些是干净的方法?
我正在尝试将两个值绑定到一个标签的内容中,中间有一个空格.我正在关注MSDN(MSDN文章)中的一个示例,但我的标签是空的.这是我的代码:
类:
public class Item
{
//Other properties removed to shorten
public string name { get; set; }
public string typeLine { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
设置项目来源:
ItemsDisplay.ItemsSource = searchResults;
Run Code Online (Sandbox Code Playgroud)
XAML:
<ItemsControl Name="ItemsDisplay">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<!-- COLUMN DEFINITIONS ETC REMOVED TO SHORTEN -->
<StackPanel Grid.Column="1">
<Label Name="ItemName" Margin="10">
<Label.Content>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="name" />
<Binding Path="typeLine" />
</MultiBinding>
</Label.Content>
</Label>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
如果我绑定一个值,它就完美地工作了
<StackPanel Grid.Column="1">
<Label Name="ItemName" Margin="10" Content="{Binding Path=name}" />
<Label Name="ItemType" …Run Code Online (Sandbox Code Playgroud)