gpt4 book ai didi

c# - 如何动态使 HierarchicalDataTemplate 中的用户控件可编辑/只读?

转载 作者:行者123 更新时间:2023-12-03 10:28:07 26 4
gpt4 key购买 nike

有谁知道如何动态更改 WPF HierarchicalDataTemplate 中控件(TextBox、ComboBox 等)的 IsReadOnly 属性?

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

我已尝试将 HierarchicalDataTemplate 中每个控件的 IsReadOnly 属性绑定(bind)到页面 ViewModel 中的预定 bool 值,但无法使绑定(bind)正常工作。任何帮助是极大的赞赏。

查看型号:

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

这里我展示了一个包含 HierarchicalDataTemplate 的 TreeView 控件。请注意,我尝试将 HierarchicalDataTemplate 中 TextBox 的 IsReadOnly 值绑定(bind)到页面 ViewModel 中的 bool “isReadOnlyBool”值。

查看:
<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>

我收到以下绑定(bind)错误:

System.Windows.Data 错误:40:BindingExpression 路径错误:'isReadOnlyBool' 属性不
在 'object' ''actions' (Name='')' 上找到。绑定(bind)表达式:路径=isReadOnlyBool;
DataItem='actions' (Name='');目标元素是'TextBox'(名称='');目标属性是“IsReadOnly”(类型“ bool ”)

最佳答案

您的模型/ View 模型的结构如何?我遇到了与此类似的问题,但我只是使用文本 block ,但有些需要粗体,具有不同的背景颜色等,因此动态绑定(bind)这些是必要的。我将代码修改为与您需要的相似,添加了 TextBoxes 而不是 TextBlocks,并且我能够使 IsReadOnly 属性正常工作。这是我的 TreeView 的 XAML,现在它已修改。

    <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>

这就是我的模型的样子。
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 中添加了它。我使用 Accounts 列表在我的 ViewModel 中创建了一个树状结构,这就是绑定(bind)到我的 ItemsSource 的内容。我将把我的 ViewModel 中的代码留给你,因为它非常丑陋,但我会发布一个可行的小样本。
        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);
}
}

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

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

enter image description here

关于c# - 如何动态使 HierarchicalDataTemplate 中的用户控件可编辑/只读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27928476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com