小编Tk1*_*993的帖子

如何为WPF页面赋予标题

可以在设计时通过XAML代码本身为WPF窗口提供标题,并在运行时显示窗口的标题.

XAML中的代码就像

Window1.Title="FormulaBuilder"
Run Code Online (Sandbox Code Playgroud)

对于WPF页面,它也在XAML代码中给出

Page1.Title="EmployeeMaster"
Run Code Online (Sandbox Code Playgroud)

但它没有在运行时显示标题

然后我尝试通过C#编码给出标题

Page1 obj=new Page1();
obj.Title="EmployeeMaster";
Run Code Online (Sandbox Code Playgroud)

但它没有在运行时显示标题.

c# wpf xaml

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

单击另一个按钮时无法获得组合框选定的项目值

我有一个组合框cmbOptions和一个按钮btnShowItem

这是代码:

private void btnShowItem_click(object sender, RoutedEventArgs e)
{
    string item = ((ComboBoxItem)cmbOptions.SelectedItem).Content.ToString(); //Exception is here
}
Run Code Online (Sandbox Code Playgroud)

以下是例外情况:

System.InvalidCastException:"无法将'System.String'类型的对象强制转换为'System.Windows.Controls.ComboBoxItem'."

我已经经历过以下的一些链接:

无法获取ComboBox所选项目值

ComboBox- SelectionChanged事件具有旧值,而不是新值

从c#wpf中的组合框中获取选定的值

等等..

但没有得到解决方案.

请注意我需要在buttonclick上获取comboboxItem的值,而不是cmbSelectionChange事件

c# wpf combobox exception

2
推荐指数
1
解决办法
1202
查看次数

WPF:在每个页面导航上更改窗口的标题

我有一个WPF application由一个窗口组成MainWindow.xaml

标题为Sample_Window

<Window
   Title="Sample_Window">
<Grid>
   <Frame x:Name="mainFrame" Source="/Page1.xaml" />
<Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

看到这张图片

和三页:

    Page1.xaml
    Page2.xaml
    Page3.xaml
Run Code Online (Sandbox Code Playgroud)

我的问题是我想在每个页面导航上更改 MainWindow 的标题

例如,当我土地Page1.xaml所有权应该是Sample_Window-Page1等。

我试过下面的一段代码XAML:(没有用)

<Page 
    WindowTitle="Sample_Window-Page1"
    Title="Page1">
</Page>
Run Code Online (Sandbox Code Playgroud)

另外,在CS 中:( 不起作用)

Page1 mypage= new Page1();
mypage.WindowTitle="Sample_Window-Page1";
Run Code Online (Sandbox Code Playgroud)

我也经历过这个问题How to give title to WPF pages

但没有得到解决我的问题。标题仍与Sample_Window相同

c# wpf xaml window

2
推荐指数
1
解决办法
2165
查看次数

为什么公共变量不允许在方法或c#中的类除外的任何块内?

我遇到一个非常奇怪的问题,当我尝试在string 类的方法中声明一个带有public关键字的类型的变量时,编译器会抛出编译时错误而没有正确的解释.

考虑下面的代码:

public class MyClass
{
    public string publicVarInClass= null;  // No error here. Works fine

    public void MyMethod()
    {    // Error shown here is "Expected } "
        public string publicVarInMethod = null;  // not allowed, but no error is shown in this line. WHY?

        try // Error shown here is Invalid token 'try' in class, struct or interface declaration
        {
        //some code here
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

根据我的考虑,当我们在方法中将变量decalre为public时,它将该方法排除为类,结构或接口.

这就是我的问题是为什么C#限制我们将变量声明为公共内部方法.有什么问题呢?

c#

2
推荐指数
1
解决办法
339
查看次数

在WPF中禁用按钮上将鼠标光标更改为"停止"指针

当我将鼠标悬停在按钮上时,如果它被禁用,我希望鼠标光标变为"停止"指针.如果启用了按钮,则以下代码正在运行,但它不适用于禁用的按钮:

XAML:

<Button x:Name="Button1" Content="Button1" Isenabled="false" />
Run Code Online (Sandbox Code Playgroud)

CS:

Button1.MouseEnter += (s, e) => Mouse.OverrideCursor = Cursors.No;
Run Code Online (Sandbox Code Playgroud)

.net c# wpf xaml

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

标签 统计

c# ×5

wpf ×4

xaml ×3

.net ×1

combobox ×1

exception ×1

window ×1