如何动态地使HierarchicalDataTemplate中的用户控件可编辑/只读? [英] How do I dynamically make user controls in a HierarchicalDataTemplate editable/read-only?

查看:84
本文介绍了如何动态地使HierarchicalDataTemplate中的用户控件可编辑/只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在WPF HierarchicalDataTemplate中动态更改控件(文本框,组合框等)的IsReadOnly属性吗?

Does anyone know how to change the IsReadOnly property of controls (TextBox, ComboBox, etc.) in a WPF HierarchicalDataTemplate dynamically?

我希望能够使HierarchicalDataTemplate中包含的控件对某些用户是可编辑的,而对于其他用户则是只读的.

I want to be able to make the controls contained in the HierarchicalDataTemplate editable for some users and read-only for others.

我尝试将HierarchicalDataTemplate中每个控件上的IsReadOnly属性绑定到页面的ViewModel中的预定布尔值,但是无法使绑定起作用.任何帮助是极大的赞赏.

I have tried binding the IsReadOnly property on each control in the HierarchicalDataTemplate to a predetermined Boolean value in the page's ViewModel, but am unable to get the binding to work. Any help is greatly appreciated.

VIEWMODEL:

private bool _isReadOnlyBool;
public bool isReadOnlyBool
{
    get { return _isReadOnlyBool; }
    set
    {
        _isReadOnlyBool = value;
        RaiseChange("isReadOnlyBool");
    }
}

在这里,我显示了一个包含HierarchicalDataTemplate的TreeView控件.请注意,我试图将HierarchicalDataTemplate中TextBox的IsReadOnly值绑定到页面的ViewModel的布尔值"isReadOnlyBool"值.

Here I show a TreeView control containing a HierarchicalDataTemplate. Notice that I attempt to Bind to the IsReadOnly value of the TextBox in the HierarchicalDataTemplate, to the Boolean "isReadOnlyBool" value from the page's ViewModel.

查看:

<TreeView HorizontalAlignment="Center" x:Name="treeView1" VerticalAlignment="Top" ItemsSource="{Binding Path=rsParentChild}"  Background="Transparent" BorderThickness="0" BorderBrush="Transparent" >
<TreeView.ItemContainerStyle>
    <Style>
        <Setter Property="TreeViewItem.IsExpanded" Value="True"/>
    </Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=rsParentChild, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
        <Grid Focusable="False" Margin="5,10,5,10">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Label Content="Action Text" FontSize="8" Grid.Row="0" Grid.Column="0"/>
            <TextBox Grid.Row="1" Grid.Column="0" 
             IsReadOnly="{Binding isReadOnlyBool, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" 
             Background="#99FFFFFF" 
             BorderBrush="Black" 
             Text="{Binding Path=actionText, Mode=TwoWay}" 
             TextWrapping="Wrap" Margin="0,0,0,0" 
             LostFocus="TextBox_LostFocus" 
            />
        </Grid>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

我收到以下绑定错误:

System.Windows.Data错误:40:BindingExpression路径错误:'isReadOnlyBool'属性不正确 在对象",动作"(名称=)"上找到. BindingExpression:Path = isReadOnlyBool; DataItem ='actions'(Name ='');目标元素是'TextBox'(Name ='');目标属性为"IsReadOnly"(类型为布尔")

System.Windows.Data Error: 40 : BindingExpression path error: 'isReadOnlyBool' property not found on 'object' ''actions' (Name='')'. BindingExpression:Path=isReadOnlyBool; DataItem='actions' (Name=''); target element is 'TextBox' (Name=''); target property is 'IsReadOnly' (type 'Boolean')

推荐答案

您的模型/视图模型的结构如何?我有一个与此类似的问题,但我只是使用文本块,但有些块需要加粗,具有不同的背景色等,因此必须动态绑定这些块.我修改了代码使其与您所需的代码相似,添加了TextBoxes而不是TextBlocks,从而使IsReadOnly属性能够正常工作.修改后,这是TreeView的XAML.

How is your model/viewmodel structured? I had a problem similar to this, but I was just using textblocks instead, but some needed to be bold, have different background colors, etc, so binding those dynamically was necessary. I modified my code to be similar to what you would need, adding TextBoxes instead of TextBlocks, and I was able to get the IsReadOnly property working. This is the XAML for my TreeView looks like now that it's modified.

    <TreeView x:Name="Tree" ItemsSource="{Binding Account}"  Margin="-2,45,-4,-18" BorderThickness="0,0,3,0" BorderBrush="{x:Null}" MouseDoubleClick="TreeViewItem_MouseDoubleClick_1">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Account}" DataType="{x:Type local2:Accounts}">
                    <TextBox Text="{Binding Header}" IsReadOnly="{Binding IsReadOnly}" />
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
   </TreeView>

这就是我的模型的样子.

An this is what my Model looks like.

public class Accounts
{
    private readonly List<Accounts> accounts;
    public Accounts()
    {
        accounts = new List<Accounts>();
    }
    public bool IsNodeExpanded { get; set; }
    public string Header { get; set; }
    public Brush Foreground { get; set; }
    public Brush Background { get; set; }
    public FontWeight FontWeight { get; set; }
    public string Parent { get; set; }
    public bool IsReadOnly { get; set; }
    public List<Accounts> Account
    {
        get { return accounts; }
    }

}

您可以看到我根据需要添加了属性,出于我的目的,我需要除IsReadOnly之外的所有内容.我在TextBox中添加了它.我使用帐户"列表在ViewModel中创建了一个类似树的结构,这就是绑定到我的ItemsSource上的东西.我会从ViewModel中省去代码,因为它很丑陋,但我会发布一些可行的示例.

You can see that I have properties added as necessary, for my purpose I needed everything except IsReadOnly. I added that in for the TextBox. I used the Accounts list to create a tree like structure in my ViewModel, and that is what is bound to my ItemsSource. I'll spare you the code from my ViewModel because it's pretty ugly, but I'll post a small sample of something that would work.

        private List<Accounts> accounts;
        public List<Accounts> Account
        {
            get { return accounts; }
            set
            {
                accounts = value;
                NotifyPropertyChanged("Accounts");
            }
        }

        void SetTree()
        {
            Account.Add(new Accounts { Header = "Accounts", IsReadOnly = true });
            Account[0].Account.Add(new Accounts { Header = "Product Type", Foreground = fGround, FontWeight = FontWeights.Bold, IsReadOnly = true });
            SortedSet<string> uniqueProductType = GetUniqueItems("Product Type");
            Accounts tempAccount;
            for (int i = 0; i < uniqueProductType.Count(); i++)
            {
                tempAccount = new Accounts { Header = uniqueProductType.ElementAt(i), Foreground = fGround, FontWeight = FontWeights.Normal };
                accountsSystemNode.Add(uniqueProductType.ElementAt(i), tempAccount);
                tempAccount.Account.Add(new Accounts { Header = "Client Preferred Account Name", Foreground = fGround, FontWeight = FontWeights.Bold, IsReadOnly = true });
                Account[0].Account[0].Account.Add(tempAccount);
            }
        }

要给此代码一些上下文,我的树以标题"Accounts"开头,然后给出一组子类别.这些子类别之一是产品类型". Account [0]是"Accounts","Account"的节点是Account [0] [0],"Product Type".然后,通过循环浏览我拥有的产品类型列表来填充产品类型,创建一个新的Account对象并设置必要的值,然后将其添加到"Product Type"节点中.请注意,我没有为它们设置IsReadOnly值.这就是我验证其工作方式的方式.对于每个子类别标题,我都将IsReadOnly属性设置为true,并且无法对其进行编辑,而该子类别中的实际值IsReadOnly为false,并且我可以编辑这些值.

To give some context to this code, my Tree starts with a title, "Accounts", and then gives a group of subcategories. One of these subcategories is "Product Type". Account[0] is "Accounts", and a node of "Account" is Account[0][0], "Product Type". Then I populate product type by cycling through the list of product types I have, create a new Account object and set the necessary values, and add that to my "Product Type" node. Notice I don’t have the IsReadOnly value set for these. This is how I verified it works. For each Subcategory title I set the IsReadOnly property to true and could not edit them, while the actual values within this subcategory, IsReadOnly is false, and I was able to edit those values.

这是这棵树的样子.如您所见,我能够编辑这些文本框,但无法编辑帐户"或产品类型".

This is what this tree would look like. I was able to edit those text boxes as you can see, but I wasn't able to edit "Accounts" or "Product Type".

这篇关于如何动态地使HierarchicalDataTemplate中的用户控件可编辑/只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆