我在DataGrid的资源中绑定值时遇到问题.在资源之外 - 标签它完美地工作,但在里面它不起作用.我想Datacontext可能已更改或为null.我不知道该怎么办.我读了一些关于freezables的内容,但我也没有让它们起作用.这是解决方案还是那个,我做不到的.这里我的代码与非工作和工作部分 - 仅用于演示.如果点击标题行,我需要在Resources-Section中使用Contextmenu才能获取它.
<UserControl x:Class="testapp.test.testManager.Window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:testapp.test.testManager"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="600"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<Grid>
<DataGrid ItemsSource="{Binding Lst, UpdateSourceTrigger=PropertyChanged}"
AutoGeneratingColumn="dg_AutoGeneratingColumn">
<DataGrid.Resources>
<ContextMenu x:Key="DataGridColumnHeaderContextMenu">
<MenuItem Header="{StaticResource General}">
<!-- HERE the Binding cannot find "TestCheck" -->
<CheckBox Content="Testentry Header" IsChecked="{Binding TestCheck, UpdateSourceTrigger=PropertyChanged,Mode=OneWay}"/>
<!-- ... --->
</MenuItem>
</ContextMenu>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="ContextMenu" Value="{StaticResource DataGridColumnHeaderContextMenu}" />
</Style>
</DataGrid.Resources>
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="{StaticResource General}">
<!-- Here the Binding can find "TestCheck" -->
<CheckBox Content="Testentry" IsChecked="{Binding TestCheck, UpdateSourceTrigger=PropertyChanged,Mode=OneWay}"/>
<!-- ... --> …
Run Code Online (Sandbox Code Playgroud) 我有以下问题:我有一个Listview
,其中每个项目都是一个自定义控件(“ DisruptionIcon”),它显示一个图标,具体取决于为该自定义控件设置的属性。图标可以具有多种形状(这里的示例中为“无”和“方形”)。现在的问题是,如果我向下滚动ListView
并在之后备份,则图标是错误的。结果是:
向下滚动之前:
上下滚动后:
资源是:
MainPage.xaml:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Name="MainPg"
>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView
Width="200"
ItemsSource="{Binding LineList, ElementName=MainPg}"
>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<local:DisruptionIcon
DisplayName="{Binding DisplayName}"
IconType="{Binding DisplayStyle}"
Color1="{Binding Color1}"
Color2="{Binding Color2}"
/>
<TextBlock Text="{Binding DisplayName}" Foreground="Red"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
Run Code Online (Sandbox Code Playgroud)
MainPage.xaml.cs:
public sealed partial class MainPage : Page
{
public MainPage()
{
LineList = new ObservableCollection<ServerLineDefinition>();
for (byte i = 0; i <= 250; i++)
{
var ds = …
Run Code Online (Sandbox Code Playgroud) 我有一个TreeView,它被修改为在文本前面显示图像.所以我修改后的TreeViewItem被称为ImagedTreeViewItem.此ImagedTreeViewItem有一个Property,其中包含要显示的Image-Control图像.ImagedTreeViewItem还有一个属性,用于检查ImagedTreeViewItem-Icon是否为文件夹-Icon.此属性的名称为"IsFolder".
我的问题是:
I'm Binding the Ancestors-Property (here: The ImagedTreeViewItem)
获取我需要的数据.对于我的Image-Control,它完美地工作,对于我后来添加的上下文菜单没有.我不明白为什么,因为基本上它是同一个命令.
这是XAML中我的"ImagedTreeView"的代码:
<TreeView.Resources>
<Style TargetType="{x:Type myClasses:ImagedTreeViewItem}">
<Setter Property="HeaderedItemsControl.HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!-- Here it works!!! -->
<Image Height="16" Source="{Binding Path=Icon, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type myClasses:ImagedTreeViewItem}}}" Stretch="Fill" Width="16" />
<TextBlock Margin="5,0" Text="{Binding}" />
<StackPanel.ContextMenu>
<ContextMenu>
<!-- Here not :( -->
<MenuItem Command="my:ImagedTreeView.AddFolder" Header="Add Folder"
IsEnabled="{Binding Path=IsFolder,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type myClasses:ImagedTreeViewItem}}}">
<MenuItem.Icon>
<Image Source="folderadd16.png" />
</MenuItem.Icon>
</MenuItem>
<!-- ... -->
</ContextMenu>
</StackPanel.ContextMenu>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
Run Code Online (Sandbox Code Playgroud)
我认为这段代码中的第二个绑定找不到祖先.Visual Studio的输出窗口告诉我同样的事情:
System.Windows.Data Error: 4 …
Run Code Online (Sandbox Code Playgroud) 我尝试获取所有网络接口,我在窗口中看到所有网络适配器。如果我使用
NetworkInterface.GetAllnetworkInterfaces()
Run Code Online (Sandbox Code Playgroud)
我只得到 LAN 适配器和环回。但是我没有得到我配置的 VPN 连接。VPN 连接是我需要的东西,但我找不到获取它们的方法(即使它们已断开连接)。
是否有类似于NetworkInterface.GetAllnetworkInterfaces()
获取 VPN 连接的东西?
c# ×3
binding ×2
wpf ×2
adapter ×1
ancestor ×1
datacontext ×1
listview ×1
networking ×1
vpn ×1
xaml ×1