我在我的WPF应用程序中有以下功能,用于将窗口调整为主屏幕的工作区域(整个屏幕减去任务栏):
private void Window_Loaded(object sender, RoutedEventArgs e)
{
int theHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
int theWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
this.MaxHeight = theHeight;
this.MinHeight = theHeight;
this.MaxWidth = theWidth;
this.MinWidth = theWidth;
this.Height = theHeight;
this.Width = theWidth;
this.Top = 0;
this.Left = 0;
}
Run Code Online (Sandbox Code Playgroud)
只要机器的DPI设置为100%,这种方法效果很好.但是,如果他们将DPI设置得更高,那么这不起作用,并且窗口会溢出屏幕.我意识到这是因为WPF像素与"真实"屏幕像素不同,并且因为我使用WinForms属性来获取屏幕尺寸.
我不知道WPF等效于Screen.PrimaryScreen.WorkingArea.我可以使用哪些东西,无论DPI设置如何都可以使用?
如果没有,那么我想我需要某种缩放,但我不知道如何确定要缩放多少.
如何修改我的功能以考虑不同的DPI设置?
顺便说一下,如果你想知道为什么我需要使用这个函数而不是最大化窗口,那是因为它是一个无边框窗口(WindowStyle ="None"),如果你最大化这种类型的窗口,它覆盖了任务栏.
使用 EPPlus 我想向 Excel 文件添加一个新工作表,但我不想删除文件中的现有工作表(如果有),我想将其作为文件中的第一张工作表插入。这是我为快速测试而编写的内容,但它会删除所有现有工作表:
using (ExcelPackage p = new ExcelPackage())
{
p.Workbook.Worksheets.Add("HubaHuba");
p.Workbook.Worksheets.MoveToStart("HubaHuba");
ExcelWorksheet ws = p.Workbook.Worksheets[1];
ws.Name = "HubaHuba";
var cell = ws.Cells[1, 1];
cell.Value = "dfsdfsdfsd";
cell = ws.Cells[1, 2];
cell.Value = "347895y5 Oh";
Byte[] bin = p.GetAsByteArray();
File.WriteAllBytes(path,bin);
}
Run Code Online (Sandbox Code Playgroud) 我需要帮助,因为我不明白为什么来自datatemplate的控件不会继承窗口资源中定义的样式.可能有一个解决方法吗?
如果有人能给我一个解决方案,我会非常感激,因为我花了很多时间找到一些东西.
特此我的例子.例如,horrizontal模板中的Texblock不对齐:
Udapte:我添加了背景颜色.样式应用于标签,但不应用于由datatemplate定义的totextblock和textbox.
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:localview="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="{x:Type TextBlock}" TargetType="TextBlock" >
<Setter Property="Background" Value="Cyan"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
<Setter Property="FontFamily" Value="Comic Sans MS"/>
</Style>
<Style x:Key="{x:Type Label}" TargetType="Label">
<Setter Property="Background" Value="Red"/>
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style x:Key="{x:Type TextBox}" TargetType="TextBox">
<Setter Property="Background" Value="Cyan"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
<Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
<localview:TemplateSelector …
Run Code Online (Sandbox Code Playgroud) 此代码检查交易对手列表是否包含电子邮件地址.然后在else语句中,有可能电子邮件地址仍然为0.我需要一段代码,当它为零时填写电子邮件地址列表.
if (counterParty == null)
{
mailAddressesOfCounterparty = new List<Email>();
Email unKnownEmail = new Email();
unKnownEmail.EmailAddress = loopPayment.ShortNameCalypso + "@NotInCounterpartyTable.nl";
mailAddressesOfCounterparty.Add(unKnownEmail);
}
else
{
mailAddressesOfCounterparty =
emailAddress.Where(ea => ea.CounterPartyId == counterParty.Id && ea.IsOptionContract == startOfGroupPayment.OptionContract).ToList();
}
Run Code Online (Sandbox Code Playgroud)
此代码需要制作电子邮件地址.只是不知道如何检查是否为零.
Email unKnownEmail = new Email();
unKnownEmail.EmailAddress = loopPayment.ShortNameCalypso + "@NotInCounterpartyTable.nl";
mailAddressesOfCounterparty.Add(unKnownEmail);
Run Code Online (Sandbox Code Playgroud)
在else中我需要添加一个可能性,当它变为零时,将电子邮件地址更改为某些东西.代码不会让我们使用if语句.
mailAddressesOfCounterparty变为零,因为尚未在数据库中添加内容.但是,在使用此应用程序时,这些信息可能会丢失.在这种情况下,我想创建一个emailaddress,它将显示无法找到它.
我在用
If(User.IsInRole("member"))
{
}
Run Code Online (Sandbox Code Playgroud)
但是无法让它在C#MVC类中工作.请注意,我没有在我能够工作的控制器中使用它.我错过了什么?代码甚至无法识别用户是什么.
我认为它可能是命名空间,但我使用了.Mvc
命名空间以及其他..
先感谢您
我开发了一个具有datagrid的应用程序,它具有不同的列标题和行的上下文菜单.现在对于标题上下文菜单,我已经过滤了datagrid列.代码片段如下.
<DataGrid Padding="3" BorderBrush="SkyBlue" BorderThickness="1" ItemsSource="{Binding Source={StaticResource cvsCoreData}}" SelectionUnit="FullRow" IsReadOnly="True" AutoGenerateColumns="False" x:Name="Data" Margin="0,5,0,28">
<DataGrid.Resources>
<ContextMenu x:Key="DataGridColumnHeaderContextMenu" >
<MenuItem Header="ABC" Click="ABC_Click" />
<MenuItem Header="EFG" Click="EFG_Click" />
<MenuItem Header="HIJ" Click="HIJ_Click" />
<MenuItem Header="KLM" Click="KLM_Click" />
</ContextMenu>
</DataGrid.Resources>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="Background" Value="SkyBlue" />
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
<Setter Property="ContextMenu"
Value="{StaticResource DataGridColumnHeaderContextMenu}" />
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTemplateColumn SortMemberPath="Key" Width="*" Header="Key ">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="block" TextWrapping="Wrap" Text="{Binding Key}">
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="*" Header="Lerm …
Run Code Online (Sandbox Code Playgroud) 我需要开发一个支持本地化的应用程序,因此我需要TextBlock
以这样一种方式格式化 a:字符串的一部分从绑定中获取一个参数,一部分从资源中获取。我是这样写的
<TextBlock Height="30" >
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} -- *Now only* {1:C}!">
<Binding Path="Description"/>
<Binding Path="Price"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
现在这只是我需要从资源字符串中获取的部分。该字符串位于资源中,但我不知道如何以这种方式创建该字符串。任何人都可以帮助我吗?
我知道使用转换器是一个解决方案,但我有很多这样的场景,因此我最终可能会得到多个转换器,这不是一个好主意。我相信使用StringFormat
我可以实现它。但我缺少一些东西。
我有这样的xaml代码:
<Grid x:Name="boardGrid">
<Grid.ContextMenu>
<ContextMenu Opacity="0.7" x:Name="menuContext">
</ContextMenu>
</Grid.ContextMenu>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我在后面的代码中生成网格项.我想要的是右键单击禁用上下文菜单打开.我想在确定条件发生时打开它.
这就是我在.cs文件中的内容:
每个对象都有unit.MouseRightButtonUp + = unit_MouseRightButton
void unit_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
if (CurrentGame.CurrentPlayer.HasTurn == false) return;
.....
ContextMenu.IsOpen = true;
}
Run Code Online (Sandbox Code Playgroud)
所以这意味着只有条件得到满足才能打开Contextmenu,但无论如何它都会打开.
我有以下问题:我的WPF项目中包含了一个文件夹。构建操作设置为:内容复制到输出目录设置为:始终复制文件夹名称:图像 我在我的 WPF .XAML 中使用了以下代码:
<Image Name="ImagesFolder" MouseWheel="ImagesFolder_MouseWheel" Width="320" Height="230" Grid.Row="1" Margin="31,215,29,136">
<Image.Source>
<BitmapImage UriSource="Images/1.png"/>
</Image.Source>
</Image>
Run Code Online (Sandbox Code Playgroud)
图像显示在设计器窗口中,因此程序找到了它,但是当我运行它时,我只是看不到它所在的位置。也尝试使用 Canvas + Border 组合。另一方面,我有时会使用双面图片。因此,如果我有 5 张图片,如下所示:1,1_2,2,3,4 我希望程序在按钮单击事件上使用某种代码旋转我的图片。怎么做(每张图片背景都是前景的实际图片数+前景的_实际图片数)?感谢您的帮助!
我想使用c#使用comboboxcolumns创建一个datagridview.
问题是我不知道如何为每行中的组合框提供不同的值.
DataTable dt = new DataTable();
dt.Columns.Add("state");
dt.Columns.Add("city");
dt.Rows.Add("a1", "b1");
dt.Rows.Add("a1", "b2");
dt.Rows.Add("a2", "b3");
dt.Rows.Add("a2", "b4");
dt.Rows.Add("a3", "b5");
DataGridViewComboBoxColumn comboStates = new DataGridViewComboBoxColumn();
comboStates.HeaderText = "HeaderText_1";
this.dataGridView1.Columns.Insert(0, comboStates);
DataGridViewComboBoxColumn comboCities = new DataGridViewComboBoxColumn();
comboCities.HeaderText = "HeaderText_2";
this.dataGridView1.Columns.Insert(1, comboCities);
for (int i = 0; i < dt.Rows.Count; i++)
{
dataGridView1.Rows.Add();
comboStates.Items.Add(dt.Rows[i][0]);
DataGridViewComboBoxCell stateCell = (DataGridViewComboBoxCell) (dataGridView1.Rows[i].Cells[0]);
stateCell.Value = comboStates.Items[i];
comboCities.Items.Add(dt.Rows[i][1]);
DataGridViewComboBoxCell cityCell = (DataGridViewComboBoxCell)(dataGridView1.Rows[i].Cells[1]);
cityCell.Value = comboCities.Items[i];
}
Run Code Online (Sandbox Code Playgroud)
此示例提供以下结果:
对于每一行:
comboboxcolumnstate:
a1
a1
a2
a2
a3
Run Code Online (Sandbox Code Playgroud)
comboboxcolumncity:
b1
b2
b3 …
Run Code Online (Sandbox Code Playgroud) c# ×10
wpf ×6
xaml ×5
.net ×2
asp.net-mvc ×1
contextmenu ×1
datagrid ×1
dpi ×1
epplus ×1
memory-leaks ×1
windows ×1
winforms ×1