我的节目由一个TreeView
和两个contentPresenters
地面组成.mainWindow,TreeView
和每个contentPresenter
都有自己的viewModels.
我想调用一个函数在mainWindowViewModel
从TreeViewViewModel
.
我需要这样做,因为mainWindowViewModel
控件显示的内容contentPresenters
,我想手动更新显示.
我猜我会做这样的事......
TreeViewViewModel
:
public class TreeViewViewModel
{
//Do I need to declare the MainWindowVM?
public TreeViewViewModel() { ... }
private void function()
{
//Command that affects display
//Manually call function in MainWindowVM to refresh View
}
}
Run Code Online (Sandbox Code Playgroud)
我试图通过使用来访问MainWindowVM
来自TreeViewViewModel
:
public MainWindowViewModel ViewModel { get { return DataContext as MainWindowViewModel; } }
Run Code Online (Sandbox Code Playgroud)
但它没有多大意义.因为MWVM不是DataContext
的TreeViewViewModel
.
我的程序的主菜单采用的是ContextMenu
组成MenuItems
.在我的程序(使用资源字典)的定位,我设置DynamicResource
为Header
我的每一个MenuItems
.奇怪的DynamicResource
编译,但似乎并没有影响本地化过程中的任何变化(语言上Headers
没有变化).
例子MenuItem
:
//I'm not sure if the x:Name or the PlacementRectangle is interfering with anything...
<ContextMenu x:Name="MainContextMenu" PlacementRectangle="{Binding RelativeSource={RelativeSource Self}}">
<MenuItem Header="{DynamicResource open}" />
</ContextMenu>
Run Code Online (Sandbox Code Playgroud)
MenuItem
控制的限制是什么?它应该合作DynamicResource
吗?我的总体目标是本地化这些strings
,我该怎么做?
该程序在WPF中.谢谢.
更新: 这是我的App.xaml文件中引用我的资源字典的方式:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Lang.en-US.xaml" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<Application.Resources>
Run Code Online (Sandbox Code Playgroud)
更新2: 我的英语资源字典中的示例字符串:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="open">Open</sys:String>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
更新3: 我如何将当前资源字典更改为西班牙语的示例函数:
private void spanishChange_Click(object sender, RoutedEventArgs e)
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(
(ResourceDictionary)Application.LoadComponent(new …
Run Code Online (Sandbox Code Playgroud) 目前我正在尝试使用JSlider
in Java
来控制PC的主音量.到目前为止,我已经能够找到我的PC主扬声器的端口.但是,我不知道如何成功调用它并运行getValue()或setValue()等方法.
我使用以下代码在我的电脑上搜索所有音频设备:
Mixer.Info [] mixers = AudioSystem.getMixerInfo();
System.out.println("There are " + mixers.length + " mixer info objects");
for (Mixer.Info mixerInfo : mixers)
{
System.out.println("mixer name: " + mixerInfo.getName());
Mixer mixer = AudioSystem.getMixer(mixerInfo);
Line.Info [] lineInfos = mixer.getTargetLineInfo(); // target, not source
for (Line.Info lineInfo : lineInfos)
{
System.out.println(" Line.Info: " + lineInfo);
Line line = null;
boolean opened = true;
try
{
line = mixer.getLine(lineInfo);
opened = line.isOpen() || line instanceof Clip;
if (!opened)
{
line.open(); …
Run Code Online (Sandbox Code Playgroud) 我有一个DataGrid
使用AlternatingRowBackground
,使自己更容易阅读.在同一网格我也有根据行的背景颜色变化"IsMouseOver"
Setter Property
在我的App.xaml
文件.我遇到的问题是,"IsMouseOver"
当鼠标悬停在颜色上时,具有交替颜色的行(它们不是白色)不会改变颜色.基本上AlternatingRowBackground
颜色优先于我的颜色RowStyle
.当鼠标悬停在彩色行上时,如何使彩色行也发生变化?
App.xaml中:
<!-- DataGrid Row Style -->
<Style x:Key="RowStyleWithAlternation" TargetType="DataGridRow">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Background" Value="GhostWhite"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="ContextMenu" Value="{x:Null}"/>
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Background" Value="#FFD0D0E0"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Purple"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#F9F99F" />
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
用户控制xaml:
<DataGrid ... AlternatingRowBackground="Gray" RowStyle="{StaticResource RowStyleWithAlternation}" ... />
Run Code Online (Sandbox Code Playgroud) 我在WPF中有一个自定义按钮,我想将其用作主菜单按钮,以用于样式原因.我正在尝试创建类似于Microsoft Office 2007的菜单.我已经在按钮中添加了菜单项,但是当我点击按钮时菜单没有打开.事实上,我得到一个编译器错误,说Content
已经设置了太多次.我想这可能与我的形象有关.我需要在我的按钮中添加或更改其他代码以使其在单击时实际打开菜单,没有错误?
这就是我现在按下的按钮:
<!--- MAIN MENU BUTTON -->
<Button Width="50"
Height="50" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top">
<!--- MAIN MENU BUTTON IMAGE -->
<Button.Content>
<StackPanel Orientation="Horizontal">
<Image
Source="..."
Width="40"
Height="40" />
</StackPanel>
</Button.Content>
<!-- MENU COMMAND -->
<Menu>
<MenuItem x:Name="MainMenu">
<MenuItem Header="New" />
<MenuItem Header="Open" />
<MenuItem Header="Exit" Click="Exit_Click" />
</MenuItem>
</Menu>
</Button>
Run Code Online (Sandbox Code Playgroud)
我已经阅读了关于绑定的示例,但我不确定如何在这个特定的实例中做到这一点.
谢谢.
我正试图在c#中设置TreeViewItem
- >的属性,StackPanel
就像这个问题一样.它似乎很有意义,直到我到达我尝试编辑Background
我的部分Border
.它们中Borders
有Background
物体,但对于我的生命,我无法设置颜色或任何东西.它似乎是不一致的,因为我可以通过简单地说,添加Content
到a .Label
Content = "Title"
无论如何,这是我的代码:
public static TreeViewItem childNode = new TreeViewItem() //Child Node
{
Header = new StackPanel
{
Orientation = Orientation.Horizontal,
Children =
{
new Border {
Width = 12,
Height = 14,
Background = ? //How do I set the background?
},
new Label {
Content = "Child1"
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
PS - 我在尝试添加时遇到同样的问题 BorderBrush
谢谢!
在我的程序中,我有一个用户控件,它使用内容展示器在窗口上显示数据。我想textBox
在启动时简单地将光标焦点设置在我的窗口中的某个位置。
通常我会通过窗口的代码隐藏来做到这一点,如下所示: textBox.Focus();
但是,textBox
是在用户控件中定义的,并且工作方式似乎不同。到目前为止,我已经在用户控件的代码隐藏中尝试了与上面相同的方法。
为什么这不起作用?如果textBox
是在用户控件中定义的,如何设置焦点?
我尝试过的....:
用户控制:
public UserControl()
{
InitializeComponent();
FocusManager.SetFocusedElement(this, textBox);
}
Run Code Online (Sandbox Code Playgroud)
用户控制:
public UserControl()
{
InitializeComponent();
textBox.Focusable = true;
Keyboard.Focus(textBox);
}
Run Code Online (Sandbox Code Playgroud) 我想知道如何选择tabItem
一个tabControl
名字的特定字符串值相匹配.我猜我将不得不进行某种搜索.
这是一个直观的例子:
string selectedTabItem = "TabItem";
//if there exists a Tab Item in this specific tab control
//with the above string as it's Name
//that Tab Item .IsSelected = true;
Run Code Online (Sandbox Code Playgroud) 我有一个WPF GUI,允许用户打开选项菜单.选项菜单在新窗口中打开,并填充复选框.当用户按下"确定"按钮时,窗口关闭.但是,它不记得在打开备份时检查了哪些复选框.如何确保程序能够记住检查了哪些盒子以及哪些盒子没有?
只需指定:我只需要记住在程序运行期间检查哪些框.整个程序退出后,程序不需要记住.
谢谢!
这是我的主窗口Window1.XAML.CS下的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CartToolsPrototype1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
//Exit
private void Exit_Click(object sender, RoutedEventArgs e)
{
System.Environment.Exit(0);
}
//Options
private void Options_Click(object sender, RoutedEventArgs e)
{
var newWindow = new Options();
newWindow.Show();
} …
Run Code Online (Sandbox Code Playgroud) 这个问题是对这个问题的跟进.我目前的总体目标是根据输入的值以数字升序添加到我的程序TreeViewItem
(我的TreeViewItem
运行时添加了子节点)header
.
我收到使用一个答案ModelView
,一个工具,我不是那么熟悉了,我也被告知,这可以通过使用来完成List
的TreeViewItems
.List
由于我缺乏经验,我决定探索这个选项ModelView
.
在我的研究,我了解到Lists
的TreeViewItems
有一点不同,因为你真的不喜欢你可以在引用它们array
.这使得他们更难以完成工作.我将解释我当前的方法并发布我的代码.请引导我朝着正确的方向前进,并提供编码解决方案的答案.我目前仍然坚持我的treeViewListAdd
功能.我在评论中编写了伪代码,用于解释我要对该区域进行的操作.
*注意:我是TreeViewItem
从一个单独的窗口添加到我的
现在我的添加TreeViewItem
过程包括:
if
不是数字,break
操作(DONE)else
- 继续添加子项(DONE)if
找到重复break
操作(DONE)else
- 继续(完成)List
的TreeViewItem
(DONE -但没有实现)TreeViewItem
为新子节点创建(DONE)header
是根据用户文字设置的textBox
(DONE)List
数字顺序的函数(问题区域)List
到TreeViewItem
主窗口 …c# ×8
wpf ×8
xaml ×2
audio ×1
background ×1
button ×1
childwindow ×1
datagrid ×1
java ×1
javasound ×1
list ×1
localization ×1
menuitem ×1
mvvm ×1
search ×1
sorting ×1
stackpanel ×1
tabcontrol ×1
textbox ×1
treeviewitem ×1
viewmodel ×1