小编Rac*_*hel的帖子

WPF - Combobox SelectedItem没有设置?

我有一个ComboBox,它的ItemsSource绑定到一个静态List<CustomSettings>的选项.ComboBox是绑定到CustomObject类的表单的一部分,该类的一个属性是CustomSettingProperty.

我想将ComboBox的SelectedItem绑定到CustomObject中指定的属性,但是SelectedItem="{Binding Path=CustomSettingProperty}"没有设置默认的选定项.使用断点我可以看到它正在调用get;方法,所以我认为问题可能在于CustomSettingProperty是单独创建的,List<CustomObject>因此WPF认为它不是同一个项目.

是否有捷径可寻?或者也许是另一种选择,因为CustomSettings类确实包含Id?

wpf binding combobox selecteditem itemssource

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

WPF ObservableCollection <T> vs BindingList <T>

在我的WPF应用程序中,我有一个XamDataGrid.网格绑定到ObservableCollection.我需要允许用户通过网格插入新行,但事实证明,为了使"添加新行"行可用,xamDataGrid的源需要实现IBindingList.ObservableCollection不实现该接口.

如果我将我的源代码更改为BindingList,它可以正常工作.但是,根据我在阅读这个主题时可以理解的,BindingList实际上是一个WinForms的东西,在WPF中并不完全支持.

如果我将所有ObservableCollections更改为BindingLists,我会犯错吗?有没有人有任何其他建议,我如何为我的xamDataGrid添加新的行功能,同时将源保持为ObservableCollection?我的理解是,有许多不同的网格需要实现IBindingList才能支持添加新的行功能,但我看到的大多数解决方案只是切换到BindingList.

谢谢.

c# data-binding wpf infragistics observablecollection

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

WPF - 在TabControl和ZIndex中重叠自定义选项卡

问题
我有一个自定义标签控件,使用与ViewModel绑定的Chrome形状标签.由于形状,边缘重叠一点.我有一个函数设置tabItem的ZIndex,TabControl_SelectionChanged它可以正常选择选项卡和拖放选项卡,但是当我通过中继命令添加或关闭选项卡时,我得到了不寻常的结果.有没有人有任何想法?

默认视图:

删除标签:

连续添加2个或更多Tab:

一次添加超过1个选项卡将不会重置其他最近添加的选项卡的zindex,因此它们会在右侧的选项卡后面,并且关闭选项卡无法正确呈现替换它的SelectedTab的ZIndex并且它显示在选项卡右侧.

用于设置ZIndex的代码

private void PrimaryTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.Source is TabControl)
        {
            TabControl tabControl = sender as TabControl;
            ItemContainerGenerator icg = tabControl.ItemContainerGenerator;
            if (icg.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
            {
                foreach (object o in tabControl.Items)
                {
                    UIElement tabItem = icg.ContainerFromItem(o) as UIElement;
                    Panel.SetZIndex(tabItem, (o == tabControl.SelectedItem ? 100 :
                        90 - tabControl.Items.IndexOf(o)));
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

通过使用断点,我可以看到它正确地将ZIndex设置为我想要的,但布局不显示更改.我知道有些变化是有效的,因为如果它们都没有工作,那么标签边缘将被颠倒(右边的标签将被绘制在左边的标签之上).单击选项卡将正确设置所有选项卡的zindex(包括应在顶部绘制的选项卡),拖放它们以重新排列它们也可以正确渲染(删除并重新插入选项卡项).我能想到的唯一区别是我使用MVVM设计模式和添加/关闭选项卡是中继命令的按钮.

有谁知道为什么会发生这种情况以及如何解决它?

ps我尝试在我的ViewModel中设置一个ZIndex并绑定到它,但是当通过relay命令添加/删除选项卡时会发生同样的事情.

wpf tabcontrol z-index tabitem

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

是否可以创建一个通用的Int-to-Enum转换器?

我想能说

<DataTrigger Binding="{Binding SomeIntValue}" 
             Value="{x:Static local:MyEnum.SomeValue}">
Run Code Online (Sandbox Code Playgroud)

并让它解决,就True好像int值等于(int)MyEnum.Value

我知道我可以Converter返回(MyEnum)intValue,但是我必须为我在DataTriggers中使用的每个Enum类型创建一个转换器.

有没有一种通用的方法来创建一个能够提供这种功能的转换器?

c# wpf enums converter datatrigger

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

如何覆盖ListBox的ItemTemplate并仍保留DisplayMemberPath?

我有一个通用的样式ListBox覆盖ItemTemplate使用RadioButtons.它工作得很好,除了我设置时DisplayMemberPath.然后我就得到了.ToString()该项目的内容ListBox.

我觉得我在这里缺少一些简单的东西......有人可以帮助我发现它吗?

<Style x:Key="RadioButtonListBoxStyle" TargetType="{x:Type ListBox}">
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}" >
                <Setter Property="Margin" Value="2, 2, 2, 0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border Background="Transparent">
                                <RadioButton
                                    Content="{TemplateBinding ContentPresenter.Content}" VerticalAlignment="Center"
                                    IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>

                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

ListBox绑定到List<T>KeyValuePairs.如果我删除了样式,则DisplayMemberPath显示正确,因此它必须是样式的东西.

<ListBox Style="{StaticResource RadioButtonListBoxStyle}"
         ItemsSource="{Binding MyCollection}"
         DisplayMemberPath="Value" SelectedValuePath="Key" />
Run Code Online (Sandbox Code Playgroud)

wpf xaml templates listbox

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

如何防止Tab键到UserControl?

我有一个自定义弹出窗口覆盖我的部分屏幕.当它打开时,我想禁用Tabbing到它后面的UserControl.我不想使用该IsEnabled属性,因为我不想灰显所有控件.

还有另一个属性做同样的事情吗?IsTabStop仅阻止选项卡在UserControl本身上停止,而不是它的子项,并且IsFocusable不是UserControl的有效属性.

wpf xaml focus keyboard-navigation

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

我可以在不知道绑定本身的情况下更改DataTrigger中绑定的属性吗?

TextBox如果盒子没有聚焦,我有一种格式化数字的样式,但是在编辑时保留未格式化的数字.

这是我想要多个TextBoxes的样式,但它们都包含不同的Text绑定.常规文本设置器和触发文本设置器之间的唯一区别是触发器具有StringFormat=N2绑定.

有没有办法让这种风格变得通用,例如只改变StringFormatDataTrigger中绑定的属性?

<TextBox>
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
                <Setter Property="Text" Value="{Binding SomeValue, StringFormat=N2, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter Property="Text" Value="{Binding SomeValue, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
Run Code Online (Sandbox Code Playgroud)

data-binding wpf xaml styles

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

使用PrincipalContext.ValidateCredentials对本地计算机进行身份验证时出错?

我有一个WCF服务,其中包含一个Login根据本地计算机凭据验证用户名和密码的方法,并且在看似随机的一段时间后,它将停止为某些用户工作.

实际的登录命令如下所示:

public UserModel Login(string username, string password, string ipAddress)
{
    // Verify valid parameters
    if (username == null || password == null)
        return null;

    try
    {
        using (var pContext = new PrincipalContext(ContextType.Machine))
        {
            // Authenticate against local machine
            if (pContext.ValidateCredentials(username, password))
            {
                // Authenticate user against database
                using (var context = new MyEntities(Connections.GetConnectionString()))
                {
                    var user = (from u in context.Users
                                where u.LoginName.ToUpper() == username.ToUpper()
                                      && u.IsActive == true
                                      && (string.IsNullOrEmpty(u.AllowedIpAddresses)
                                        || u.AllowedIpAddresses.Contains(ipAddress))
                                select u).FirstOrDefault();

                    // If …
Run Code Online (Sandbox Code Playgroud)

c# authentication wcf active-directory principalcontext

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

WPF - ControlTemplate上的事件?

有谁知道为什么我不能在控件模板上设置事件?

例如,以下代码行将无法编译.它通过控件模板中的任何事件执行此操作.

<ControlTemplate x:Key="DefaultTemplate" TargetType="ContentControl">
   <StackPanel Loaded="StackPanel_Loaded">

   </StackPanel>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

我使用的是MVVM设计模式,此处的控件位于ResourceDictionary中,该ResourceDictionary被添加到应用程序的MergedDictionaries中.

wpf controltemplate routed-events

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

为什么这个额外的空间出现在网格中?

我正在看这个问题并发现了一些非常奇怪的东西:在某些情况下,似乎错误地计算了一行的高度Grid.RowSpan.

这是Grid我正在测试的简单图纸:

---------------
|   1   |     |
--------|  3  |
|   2   |     |
---------------
|      4      |
---------------

以下是此网格的一些示例代码,用于演示此问题:

<Grid ShowGridLines="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackPanel Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Background="Red">
        <Label Content="CELL 1 A"/>
        <Label Content="CELL 1 B"/>
        <Label Content="CELL 1 C"/>
    </StackPanel>

    <Grid Grid.Column="0" Grid.Row="2" Background="CornflowerBlue">
        <Label Content="CELL 2 D"/>
    </Grid>

    <StackPanel Grid.Column="1" Grid.Row="0" …
Run Code Online (Sandbox Code Playgroud)

wpf grid

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