小编Csu*_*enő的帖子

如何使WPF组合框具有XAML中最宽元素的宽度?

我知道如何在代码中执行此操作,但这可以在XAML中完成吗?

Window1.xaml:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <ComboBox Name="ComboBox1" HorizontalAlignment="Left" VerticalAlignment="Top">
            <ComboBoxItem>ComboBoxItem1</ComboBoxItem>
            <ComboBoxItem>ComboBoxItem2</ComboBoxItem>
        </ComboBox>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

Window1.xaml.cs:

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            double width = 0;
            foreach (ComboBoxItem item in ComboBox1.Items)
            {
                item.Measure(new Size(
                    double.PositiveInfinity, double.PositiveInfinity));
                if (item.DesiredSize.Width > width)
                    width = item.DesiredSize.Width;
            }
            ComboBox1.Measure(new Size(
                double.PositiveInfinity, double.PositiveInfinity));
            ComboBox1.Width = ComboBox1.DesiredSize.Width + width;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# wpf combobox

98
推荐指数
4
解决办法
5万
查看次数

如何以编程方式将WPF控件的颜色设置为系统颜色,以便更新颜色方案更改?

我怎么能在WPF的代码隐藏中做到这一点?

<Grid Background="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}"/>
Run Code Online (Sandbox Code Playgroud)

c# wpf

33
推荐指数
3
解决办法
3万
查看次数

如何显示超过63个字符的系统托盘工具提示?

如何显示超过63个字符的系统托盘工具提示?NotifyIcon.Text有63个字符限制,但我已经看到VNC服务器有更长的工具提示.

我该怎么做VNC服务器呢?

c# systray winforms

17
推荐指数
2
解决办法
8352
查看次数

WPF中的参数化样式/模板?

如果我有两个200行长的控制模板,只有几个字(几种颜色)不同,我怎样才能使xaml可重用?也就是说,不必复制粘贴模板并在200行中更改3个单词.

这是一个简化的例子.两种风格的唯一区别是边框颜色.那么我能以某种方式定义ButtonStyle,使用参数化颜色,并从中继承BlackButtonStyle和GrayButtonStyle,并在BlackButtonStyle和GrayButtonStyle中仅指定该颜色吗?

alt text http://img444.imageshack.us/img444/9545/buttonstyles.png

<Window x:Class="WpfApplication33.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>

        <Style x:Key="BlackButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border BorderBrush="Black" BorderThickness="3">
                            <ContentControl Content="{TemplateBinding Content}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="GrayButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border BorderBrush="Gray" BorderThickness="3">
                            <ContentControl Content="{TemplateBinding Content}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>
    <StackPanel>
        <Button Content="Black Button"
                Style="{StaticResource BlackButtonStyle}"/>
        <Button Content="Gray Button"
                Style="{StaticResource GrayButtonStyle}"/>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

这是基于2个答案的代码.只需要在控件上设置样式,但不幸的是它仍然会混淆控件的标记:

<Window x:Class="WpfApplication33.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>

        <Style x:Key="ButtonStyle" …
Run Code Online (Sandbox Code Playgroud)

wpf

10
推荐指数
2
解决办法
2050
查看次数

如何在WPF窗口中快速对齐控件?

我注意到,与Windows窗体设计器相比,WPF设计器在对齐控件方面做得很差.

在下面的窗口中,我无法对齐每个标签,因此其文本与旁边文本框中的文本位于同一行.第一个标签正确对齐,但WPF设计师没有给我任何快照线来正确对齐第二个和第三个标签.

此外,我无法将按钮与标签对齐.与标签文本相比,快照线将按钮向左放置几个像素.

我无法找到一种快速的方法来手动执行此对齐,也可以编写XAML代码.将控件放在网格中,并设置每个控件的边距非常耗时.

替代文字http://img520.imageshack.us/img520/4843/wpfdesigneralignment.png

您是否知道在WPF窗口中对齐控件的快速方法?

c# wpf

8
推荐指数
2
解决办法
2万
查看次数

如何在每个WPF窗口中处理快捷键?

我想基于一些自定义逻辑打开帮助文件到页面.如何处理用户在我的所有窗口(主窗口和模态对话框)上按F1键?

我知道如何在一个窗口中处理F1,但这可以全局完成,所以我不必在所有窗口中添加相同的代码吗?

下面是我尝试过的测试,F1在子窗口上不起作用.

Window1.xaml:

<Window x:Class="WpfApplication2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Help"
                        Executed="CommandBinding_Executed"/>
    </Window.CommandBindings>
    <Grid>
        <Button Content="Open a new window"
                Click="Button_Click"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

Window1.xaml.cs:

using System.Windows;
using System.Windows.Input;

namespace WpfApplication2
{
    partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Help");
        }

        void Button_Click(object sender, RoutedEventArgs e)
        {
            new Window().ShowDialog();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# wpf keyboard-shortcuts hotkeys

8
推荐指数
1
解决办法
4241
查看次数

仅当我没有英文字符串表时,LoadString才有效

我希望能够以编程方式修改应用程序的语言,或者至少使用控制面板中指定的语言 - >区域和语言选项 - >格式.

如果我添加一个英语字符串表,制作一个法语和德语副本,并删除英语字符串,我可以编程方式在加载法语和德语字符串之间切换.如果我保留英文副本,无论如何,当我尝试加载德语或法语时,英语字符串都会被加载.

我认为这是一个资源加载器错误,如果资源加载器找到与windows ui语言相同语言的字符串表(例如,Windows资源管理器菜单的语言),则忽略SetThreadLocale.

我尝试将控制面板 - >区域和语言选项 - >格式更改为法语,但这没有任何效果.资源编辑器显示没有附加语言的法语字符串表,但我的程序仍然总是加载英语字符串.将此更改复制到系统帐户也不起作用.

这是我试过的代码:

#include "stdafx.h"
#include <iostream>
#include "windows.h" // this should go to stdafx.h
#include "resource.h" // this should not go to stdafx.h
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    // 1036 = french, 1031 = german
    SetThreadLocale(MAKELCID(1036, SORT_DEFAULT));
    const int maxSize = 100;
    wchar_t c[maxSize];
    LoadString(GetModuleHandle(NULL), IDS_STRING101, c, maxSize);
    std::cout << c;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

是一个错误的,不完整的解释(在方法2的后半部分).在那里提出的第二种解决方法,只使用相对于中性的字符串表是没用的,因为我有单独的葡萄牙语 - 葡萄牙语和葡萄牙语 - 巴西字符串表.

提出的第一个解决方法不起作用.使用下面的代码,我得到错误1814.

HRSRC r = …
Run Code Online (Sandbox Code Playgroud)

c++ windows localization visual-c++

7
推荐指数
3
解决办法
9332
查看次数

WPF:如何避免ListBox或ListView中已选中复选框的闪烁?

如何避免WPF ListBox或ListView中已选中复选框的闪烁?通过单击"刷新"按钮或滚动列表框,可以使用以下代码复制它.如果IsChecked为false,则不会闪烁.

Window1.xaml:

<Window x:Class="WpfApplication6.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ListBox Name="listBox">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="True"
                                  VerticalAlignment="Center"/>
                        <Label Padding="3"
                               Content="{Binding}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Content="Refresh"
                Grid.Column="1"
                VerticalAlignment="Top"
                Click="Button_Click"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

Window1.xaml.cs:

using System.Windows;

namespace WpfApplication6
{
    partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            Button_Click(null, null);
        }

        void Button_Click(object sender, RoutedEventArgs e)
        {
            var items = new int[10000];
            for (int i = 0; i < items.Length; i++)
                items[i] = i …
Run Code Online (Sandbox Code Playgroud)

wpf rendering

7
推荐指数
2
解决办法
2460
查看次数

当应用程序有未保存的数据时,处理注销/关闭/重启的正确方法是什么?

在WPF App.Current.SessionEnding必须在几秒钟内返回,否则出现"应用程序没有响应"窗口.因此,在此事件处理程序中不能要求用户保存他的数据,因为用户的响应需要的时间超过几秒钟.

我认为解决方案是取消注销/关闭/重启,并在用户回答文件保存对话框时恢复它.

    ReasonSessionEnding _reasonSessionEnding;

    App.Current.SessionEnding +=
        new SessionEndingCancelEventHandler(Current_SessionEnding);

    void Current_SessionEnding(object sender, SessionEndingCancelEventArgs e)
    {
        if (_dataModified)
        {
            e.Cancel = true;
            _reasonSessionEnding = e.ReasonSessionEnding;
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(EndSession));
        }
    }

    void EndSession()
    {
        if (SaveWithConfirmation()) // if the user didn't press Cancel
            //if (_reasonSessionEnding = ReasonSessionEnding.Logoff)
                // logoff
            //else
                // shutdown or restart ?
    }
Run Code Online (Sandbox Code Playgroud)

问题是ReasonSessionEnding没有告诉我Windows是关闭还是重新启动(它没有区分两者).

那么,我的程序应该如何处理会话结束事件?它甚至应该做什么,或者在这个事件上什么都不做是标准的?

要求用户将其更改保存在我的主窗体的OnClosing方法中,这样他就不会丢失数据,但我认为"应用程序没有响应"窗口并不表示正常的工作流程.

我想,取消关机是不可取的,因为其他一些程序已经关闭了.

c# wpf shutdown

4
推荐指数
1
解决办法
2297
查看次数

跨用户控件的WPF网格?

在ASP.NET中,我可以使用户控件占用页面上的表中的多个单元格:

的UserControl1:

<tr>
  <td>foo</td>
  <td>bar</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

第1页:

<table>
  <put a UserControl1 here/>
  <put another UserControl1 here/>
</table>
Run Code Online (Sandbox Code Playgroud)

并自动调整列宽以适应最大的用户控件.

这可以在WPF中完成吗?怎么样 ?

我认为用户控件不能这样做,我必须创建一个简单的类而不是用户控件,它有一个方法将所有内容添加到网格中.但是这样一切都应该通过代码完成,xaml在这里没用.

wpf grid

3
推荐指数
1
解决办法
3697
查看次数