小编Eri*_*ark的帖子

如何从其他视图模型中调用主视图模型中的函数?

我的节目由一个TreeView和两个contentPresenters地面组成.mainWindow,TreeView和每个contentPresenter都有自己的viewModels.

我想调用一个函数在mainWindowViewModelTreeViewViewModel.

我需要这样做,因为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不是DataContextTreeViewViewModel.

c# wpf mvvm viewmodel

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

为什么MenuItems不能与DynamicResource一起使用?

我的程序的主菜单采用的是ContextMenu组成MenuItems.在我的程序(使用资源字典)的定位,我设置DynamicResourceHeader我的每一个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)

c# xaml localization menuitem dynamicresource

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

需要帮助来控制我的计算机的主输出扬声器

目前我正在尝试使用JSliderin 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)

java audio javasound

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

DataGrid - AlternatingRowBackground颜色干扰"IsMouseOver"颜色

我有一个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)

c# wpf xaml datagrid

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

如何使自定义按钮显示菜单

我在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)

我已经阅读了关于绑定的示例,但我不确定如何在这个特定的实例中做到这一点.

谢谢.

wpf button drop-down-menu

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

如何使用代码隐藏创建StackPanel - >边框 - >背景

我正试图在c#中设置TreeViewItem- >的属性,StackPanel就像这个问题一样.它似乎很有意义,直到我到达我尝试编辑Background我的部分Border.它们中BordersBackground物体,但对于我的生命,我无法设置颜色或任何东西.它似乎是不一致的,因为我可以通过简单地说,添加Content到a .LabelContent = "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

谢谢!

c# wpf background stackpanel

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

将焦点设置在 UserControl 中的 TextBox 上

在我的程序中,我有一个用户控件,它使用内容展示器在窗口上显示数据。我想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)

c# wpf user-controls textbox

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

根据字符串值在Tab Control中搜索特定的Tab项

我想知道如何选择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)

c# wpf search tabcontrol

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

在WPF C#中保存子窗口的状态

我有一个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)

c# wpf childwindow

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

在C#中按数字排序TreeViewItems列表

这个问题是对这个问题的跟进.我目前的总体目标是根据输入的值以数字升序添加到我的程序TreeViewItem(我的TreeViewItem运行时添加了子节点)header.

我收到使用一个答案ModelView,一个工具,我不是那么熟悉了,我也被告知,这可以通过使用来完成ListTreeViewItems.List由于我缺乏经验,我决定探索这个选项ModelView.

在我的研究,我了解到ListsTreeViewItems有一点不同,因为你真的不喜欢你可以在引用它们array.这使得他们更难以完成工作.我将解释我当前的方法并发布我的代码.请引导我朝着正确的方向前进,并提供编码解决方案的答案.我目前仍然坚持我的treeViewListAdd功能.我在评论中编写了伪代码,用于解释我要对该区域进行的操作.

*注意:我是TreeViewItem从一个单独的窗口添加到我的

现在我的添加TreeViewItem过程包括:

  1. 检查输入的项目是否为数字(DONE)
  2. if不是数字,break操作(DONE)
  3. else- 继续添加子项(DONE)
  4. 检查重复的孩子(DONE)
  5. if找到重复break操作(DONE)
  6. else- 继续(完成)
  7. 创建ListTreeViewItem (DONE -但没有实现)
  8. TreeViewItem为新子节点创建(DONE)
  9. TVI header是根据用户文字设置的textBox (DONE)
  10. 传递给尝试添加到List数字顺序的函数(问题区域)
  11. 添加排序ListTreeViewItem主窗口 …

c# sorting wpf list treeviewitem

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