可以在设计时通过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)
但它没有在运行时显示标题.
我有一个组合框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- SelectionChanged事件具有旧值,而不是新值
等等..
但没有得到解决方案.
请注意我需要在buttonclick上获取comboboxItem的值,而不是cmbSelectionChange事件
我有一个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相同
我遇到一个非常奇怪的问题,当我尝试在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#限制我们将变量声明为公共内部方法.有什么问题呢?
当我将鼠标悬停在按钮上时,如果它被禁用,我希望鼠标光标变为"停止"指针.如果启用了按钮,则以下代码正在运行,但它不适用于禁用的按钮:
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)