我的情况:
我正在开发一个C#WPF应用程序(在Windows上),我需要在运行时动态创建很多控件.由于应用程序的性质,我无法在WPF窗口的许多方面使用标准XAML(带有模板).这是一个非常独特的案例,不,我不会重新考虑我的申请格式.
我想要完成的事情:
我想以编程方式创建一个控件,显示一个可滚动的StackPanels 列表(或任何其他有效的控制组),对于一个用例,每个Image控件都包含一个TextBlock控件(标题/标题)上的控件(图片):
ListBox控件一样).我已经包含了一张图片(下图),为您提供了一个可能的用例示例.
在过去,我已经能够通过使用ListView带有ItemTemplate(包裹在a ScrollViewer)中使用XAML 来完成所有这些.但是,完全使用C#代码执行此操作会使其更加困难.我最近使用ControlTemplate简单的c#代码(用FrameworkElementFactorys.它可能会有点复杂,我不确定这是最好的做法.我应该尝试使用相同的路径(使用ListView模板)?如果那么,怎么样呢?或者用C#代码实现更简单,更优雅的选择?
编辑:我真的不想使用任何数据绑定.我只想创建一个StackPanels的(可滚动的)列表,我可以轻松地修改/调整它.使用数据绑定感觉就像是向后实现,并且违背了运行时动态特性的目的.
编辑2(1/25/2018):回复不多.我只需要一个统一的,可滚动的stackpanel列表.我可以调整它以满足我的需求,但它需要全部用C#(代码隐藏).如果有人需要更多信息/澄清,请告诉我.谢谢.
我目前正在开发一个C#WPF应用程序,它包含一个Grid带有控件的全屏,这些控件在运行时动态添加到它.我有代码允许用户在Grid鼠标事件中移动这些控件.我现在想要做的是允许用户也在运行时调整控件的大小(同时保持宽高比).我已经看过各种教程,描述如何使用Canvas(和Thumb控件),但没有Grid.由于我无法Canvas在我的应用程序中使用a ,有没有一种有效的方法在网格上实现它?为了让您了解我的代码是什么样的,我将鼠标事件放在下面:[编辑如下]
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
//Orientation variables:
public static Point _anchorPoint;
public static Point _currentPoint;
private static double _originalTop;
private static double _originalLeft;
private static Point _startPoint;
private static bool _isDown = false;
private static bool _isInDrag = false;
private static bool _isDragging = false;
public static UIElement selectedElement = null;
public static bool elementIsSelected = false;
public static …Run Code Online (Sandbox Code Playgroud) 所以我正在尝试使用ctypes模块创建一个Python 3.3程序来更改Windows桌面背景.我在Python 2.7中测试了以下代码,它运行得很好.但它只适用于Python 3.3!我正在使用Windows 7.这是代码:
import ctypes
SPI_SETDESKTOPWALLPAPER=20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKTOPWALLPAPER, 0,"C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg", 3)
Run Code Online (Sandbox Code Playgroud) 我在尝试为WPF窗口创建UI时遇到了很多困难.我正试图(动态)显示一堆电影海报,其中包含影片名称直接在影像下方.ItemsSource通过foreach迭代分配给Images列表.图像文件本身可能有不同的大小,但如下所示,我将设置统一的大小.
基本上,我的目标是让它看起来像这样:
到目前为止,我的代码只显示一个窗口,其中一个大的水平行(?),图像位于中心,没有标签.这是我的XAML代码:
<Window x:Name="TVWindow" x:Class="PACS_Pre_Alpha.TV"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TV" Height="746" Width="1000" ResizeMode="NoResize">
<Grid x:Name="TVGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<ListView x:Name="TvBox" HorizontalAlignment="Left" Height="648" VerticalAlignment="Top" Width="994" Grid.Row="5" Grid.Column="5">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch">
<Image Source="{Binding ImageData}" HorizontalAlignment="Center" VerticalAlignment="Top" />
<TextBlock Text="{Binding Title}" HorizontalAlignment="Center" VerticalAlignment="Bottom" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我的电影添加了这个C#代码:
foreach (string tvf in ContentFiles)
{
string ContentTitle = System.IO.Path.GetFileNameWithoutExtension(tvf);
MovieData cnt = new MovieData();
cnt.ImageData = LoadImage(ActualImage);
cnt.Title = …Run Code Online (Sandbox Code Playgroud) 我正在开发一个动态 C# WPF 应用程序(在 Windows 10 上),它使用全屏Grid. 控件在运行时动态添加到网格(在 a 中管理Dictionary<>),我最近添加了使用 a TranslateTransform(我现在怀疑其可行性)使用鼠标(也在运行时)沿网格移动控件的代码。
有没有办法在移动控件时防止控件在网格上重叠或“共享空间”?换句话说,添加某种碰撞检测。我会使用if语句来检查控制保证金范围之类的吗?我的移动事件如下所示:
主窗口.xaml.cs:
public partial class MainWindow : Window
{
// Orientation variables:
public bool _isInDrag = false;
public Dictionary<object, TranslateTransform> PointDict = new Dictionary<object, TranslateTransform();
public Point _anchorPoint;
public Point _currentPoint;
public MainWindow()
{
InitializeComponent();
}
public static void Control_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_isInDrag)
{
var element = sender as FrameworkElement;
element.ReleaseMouseCapture();
_isInDrag = false;
e.Handled = true;
} …Run Code Online (Sandbox Code Playgroud) 我想用Python 3运行Windows命令。就像os.system(“ echo hi”)一样。但是,如何运行需要管理员访问权限的命令?你怎么做到这一点?谢谢。
在 Windows 中编写我的 python 3.3 程序时,我遇到了一个小问题。我正在尝试将一些指令行写入文件以供程序执行。但是每次我 file.write() 下一行时,它都会替换上一行。我希望能够继续向该文件写入尽可能多的行。注意:使用 "\n" 似乎不起作用,因为您不知道将有多少行。请帮忙!这是我的代码(作为一个循环,我确实多次运行):
menu = 0
while menu != None:
menu = lipgui.choicebox("Select an option:", choices=["choice1", "choice2", "choice3"])
file = open("file.txt", "w")
if menu == "choice1":
text_to_write = lipgui.enterbox("Text to write:")
file.write(text_to_write)
Run Code Online (Sandbox Code Playgroud) 是否可以在 Windows 7 中禁用和启用 Python 中的网络连接?我在这里看到了一个以前的问题:如何以编程方式启用/禁用网络接口?(Windows XP),但仍然找不到解决方案!请与我分享代码!马丁的回答给了我这个: b'Index Name
\r\r\n0 WAN Miniport (SSTP) \r\r\n1 WAN Miniport (IKEv2) \r\r\n2 WAN Miniport (L2TP) \r\r\n3 WAN Miniport (PPTP) \r\r\n4 WAN Miniport (PPPOE) \r\r\n5 WAN Miniport (IPv6) \r\r\n6 WAN Miniport (Network Monitor) \r\r\n7 Realtek RTL8102E/RTL8103E Family PCI-E Fast Ethernet NIC (NDIS 6.20) \r\r\n8 WAN Miniport (IP) \r\r\n9 Microsoft ISATAP Adapter \r\r\n10 RAS Async Adapter \r\r\n11 Atheros AR5007 802.11b/g WiFi Adapter \r\r\n12 Teredo Tunneling Pseudo-Interface \r\r\n13 Microsoft ISATAP Adapter #2 \r\r\n\r\r\n'
Run Code Online (Sandbox Code Playgroud) python ×4
windows-7 ×4
wpf ×4
c# ×3
events ×2
listview ×2
python-3.3 ×2
python-3.x ×2
c ×1
ctypes ×1
dynamic ×1
file-io ×1
mouseevent ×1
resize ×1
xaml ×1